PHP & Mysql: It84 - It Elective 1 (PHP Programming)
PHP & Mysql: It84 - It Elective 1 (PHP Programming)
TOPICS
MySql Introduction
Connecting to Mysql
MySql Error Handling
Retrieving Data from a Database
Inserting Data into a Database
Deleting Data in a Database
Updating Data in a Database
IT84 – IT ELECTIVE (PHP PROGRAMMING)
OBJECTIVES
At the end of the lesson, the students should be able to:
Explain the concepts of MySQL
Determine the function that is used in handling MySQL related
errors
Manipulate MySQL database using the command update,
insert, delete and select.
Integrate MySQL in PHP scripts
Develop a dynamic website or web applications
IT84 – IT ELECTIVE (PHP PROGRAMMING)
MYSQL INTRODUCTION
MySQL is the most popular database system used with
PHP.
MySQL is a database system used on the web
MySQL is a database system that runs on a server
MySQL is ideal for both small and large applications
MySQL is very fast, reliable, and easy to use
IT84 – IT ELECTIVE (PHP PROGRAMMING)
MYSQL INTRODUCTION
MySQL supports standard SQL
MySQL compiles on a number of platforms
MySQL is free to download and use
MySQL is developed, distributed, and supported by
Oracle Corporation
MySQL is named after co-founder Monty Widenius's
daughter: My
IT84 – IT ELECTIVE (PHP PROGRAMMING)
CONNECTING TO MYSQL
Use the PHP mysqli_connect() function to open a new
connection to the MySQL server.
Syntax:
mysqli_connect(host, username, password, dbname);
Parameters Description
Host Optional. Either a host name or an IP address
Username Optional. The MySQL user name
Password Optional. The password to log in with
dbname Optional. The default database to be used when performing queries
IT84 – IT ELECTIVE (PHP PROGRAMMING)
CONNECTING TO MYSQL
Sample Code:
<?php
// Create connection
Syntax:
SELECT column_name(s) FROM table_name
Sample code:
$result = mysqli_query($con, "SELECT * FROM Persons");
while($row = mysqli_fetch_array($result)) {
echo $row[0]. " " . $row[1]; echo "<br>";
}
Output: 270093 Dela Cruz, Juan
210009 James, Mark
IT84 – IT ELECTIVE (PHP PROGRAMMING)
Sample Code:
mysqli_query($con, "INSERT INTO tbl_student (sn, name)
VALUES (‘230087', ‘Doe John’)");
Output: SN Name
270093 Dela Cruz, Juan
210009 James, Mark
230087 Doe, John
IT84 – IT ELECTIVE (PHP PROGRAMMING)
$sn = $_POST[‘sn'];
$name = $_POST['name'];
Sample code:
mysqli_query($con, "DELETE FROM tbl_student WHERE
sn='230087’");
IT84 – IT ELECTIVE (PHP PROGRAMMING)
Syntax:
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
Example code:
mysqli_query($con, "UPDATE tbl_student SET name= ‘Cruz,
John’ WHERE sn=‘270093’");
Updated record:
SN Name
270093 Cruz, Juan
210009 James, Mark
IT84 – IT ELECTIVE (PHP PROGRAMMING)
Questions?