PHP Database
PHP Database
PHP Database
Introduction to Database
PHP Connect to MySQL
PHP 5 and later can work with a MySQL database using:
MySQLi extension (the "i" stands for improved)
need send only the parameters each time, and not the whole
query
Prepared statements are very useful against SQL injections,
d - double
s - string
b - BLOB
By telling mysql what type of data to expect, we minimize the risk of SQL
injections.
Note: If we want to insert any data from external sources (like user input),
it is very important that the data is sanitized and validated.
PHP
<?php
Select Data From MySQL(procedural)
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " .
$row["lastname"]. "<br>“; }
} else {
echo "0 results";}
mysqli_close($conn);?>
PHP Select Data From MySQL(OO)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows() > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " .
$row["lastname"]. "<br>“;}
} else {
echo "0 results";}
$conn->close();
mysqli_close($conn);?>
The code in last slide explained below:
First, we set up an SQL query that selects the id, firstname and
lastname columns from the table. The next line of code runs
the query and puts the resulting data into a variable called
$result.
Then, the function num_rows() checks if there are more than
zero rows returned.
If there are more than zero rows returned, the function
fetch_assoc() puts all the results into an associative array that
we can loop through. The while() loop loops through the result
set and outputs the data from the id, firstname and lastname
columns.
PHP Delete Data From MySQL(procedural)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// sql to delete a record
$sql = "DELETE FROM MyGuests WHERE id=3";
if (mysqli_query($conn, $sql)) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . mysqli_error($conn);
}
mysqli_close($conn);?>
PHP Delete Data From MySQL(OO)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . mysqli_connect_error());
}
// sql to delete a record
$sql = "DELETE FROM MyGuests WHERE id=3";
if ($conn->query($sql)) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $conn->error;
}
mysqli_close($conn);?>
PHP Update Data in MySQL
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = mysqli_connect($servername, $username, $password,dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());}
$sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";
if (mysqli_query($conn, $sql)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($conn);}
mysqli_close($conn); ?>
PHP Update Data in MySQL
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
?
?
?
Functions Covered
mysqli_connect()
mysqli_query()
mysqli_num_rows()
mysqli_fetch_assoc()
mysqli_close()
mysqli()
Query()
num_rows()
fetch_assoc()
prepare()
bind_param()
execute()
get_result()