Chapter 9(Php & Mysql Db)
Chapter 9(Php & Mysql Db)
Chapter 9
Objectives
2
Introduction
$servername = "localhost";
$username = “root";
$password = "";
$dbname = "database_name";
// Create connection
$conn = new MySQLi($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
9
Tips
10
Close the Connection
2 Types of Queries
No Result Queries Result Queries
INSERT, DELETE, CREATE, etc… SELECT
13
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testdb";
// Create connection
$conn = new MySQLi($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error){
die("Connection failed: " . $conn->connect_error);
}
else {
echo "Connected successfully";
$sql = "INSERT INTO tblstudent (SID, SName, SMark) VALUES (2, 'Ahmed', 99)";
}
if ($conn->query($sql)==TRUE) {
echo "New record created successfully";
}
else {
echo "Error: " . $conn->error;
}
$conn->close(); 14
?>
Delete Data From a MySQL Table Using MySQLi
◼ Notice
❑ The WHERE clause specifies which record or records that should be
deleted. If you omit the WHERE clause, all records will be deleted!
15
Create the Connection File with Name (DBConnect.php)
<?php DBConnect.php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testdb";
// Create connection
$conn = new MySQLi($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error){
die("Connection failed: " . $conn->connect_error);
}
else {
echo "Connected successfully";
}
?>
16
Create the Deletion File with Name (Deletefile.php)
<?php Deletefile.php
// Include the connection file
require_once "DBConnect.php";
if ($conn->query($sql)==TRUE) {
echo "Record deleted successfully";
}
else {
echo "Error: " . $conn->error;
}
$conn->close();
?>
17
Update Data in a MySQL Table Using MySQLi
◼ Notice
❑ The WHERE clause specifies which record or records that should be
updated. If you omit the WHERE clause, all records will be updated!
18
Create the Updating File with Name (Updatefile.php)
Updatefile.php
<?php
// Include the connection file
require_once "DBConnect.php";
if ($conn->query($sql)==TRUE) {
echo "Record Updated successfully";
}
else {
echo "Error: " . $conn->error;
}
$conn->close();
?>
19
Select Data With MySQLi
◼ The SELECT statement is used to select data from one or
more tables:
❑ SELECT column_name(s) FROM table_name
◼ or we can use the * character to select ALL columns from a
table:
❑ SELECT * FROM table_name
20
Create the Select File with Name (Selectfile.php)
Selectfile.php
<?php
// Include the connection file
require_once "DBConnect.php";
// Create the Select Commend
$sql = "SELECT * FROM tblstudent";
$result= $conn->query($sql);
if ($result->num_rows >0) {
while($row=$result->FETCH_ASSOC()){
echo "SID:" . $row["SID"] . "<br>";
echo "SNAME: " . $row["SName"] . "<br>";
echo "SMark:" . $row["SMark"] . "<br>";
}}
else {
echo "0 result";
}
$conn->close();
?>
21
Prepared Statements in MySQLi
22
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error); }
// Prepare and bind
$stmt = $conn->prepare("INSERT INTO MyGuests (firstname, lastname,
email) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $firstname, $lastname, $email);
// Set parameters and execute
$firstname = "John";
$lastname = "Doe";
$email = "[email protected]";
if ($stmt->execute()) {
echo "New records created successfully";
} else {
echo "Error: " . $stmt->error;}
// Close statement and connection
$stmt->close();
$conn->close();
?> 23
Prepared Statements in MySQLi …
◼ This function binds the parameters to the SQL query and tells
the database what the parameters are "sss" argument lists the
types of data that the parameters are.
◼ The s character tells mysql that the parameter is a string.
◼ The argument may be one of four types:
❑ i - integer
❑ d - double
❑ s - string
❑ b - BLOB
◼ We must have one of these for each parameter.
24
Limit Data Selections From a MySQL Database
25
What if we want to select records 16 - 25 (inclusive)?
26