Chapter Seven
Chapter Seven
$pdo->exec($sql);
echo "Records inserted successfully.";
}
catch (PDOException $e) {
die("ERROR: Could not able to execute $sql. “ .$e->getMessage()); }
// Close connection
unset($pdo);
?>
PHP | MySQL Select Query
• The SQL SELECT statement is used to select the records from
database tables.
Syntax : The basic syntax of the select clause is
SELECT column1_name,column2_name,columnn_name FROM
table_name;
To select all columns from the table, the * character is used.
SELECT * FROM table_name;
To select all the data stored in the ‘ Data ‘ table, we will use the code
mentioned below. SELECT Query using Procedural Method :
<?php while ($row = mysqli_fetch_array($res))
$link = mysqli_connect("localhost", { echo "<tr>";
"root", "", "Mydb"); echo "<td>".$row['Firstname']."</td>";
echo "<td>".$row['Lastname']."</td>";
if ($link === false) { die("ERROR:
echo "<td>".$row['Age']."</td>";
Could not connect.
echo "</tr>";
".mysqli_connect_error());
}
} echo "</table>";
$sql = "SELECT * FROM Data"; mysqli_free_result($res);
if ($res = mysqli_query($link, $sql)) }
{ else {
if (mysqli_num_rows($res) > 0) { echo "No matching records are found.";
echo "<table>"; }}
echo "<tr>"; else {
echo "<th>Firstname</th>"; echo "ERROR: Could not able to execute
$sql. “ .mysqli_error($link);
echo "<th>Lastname</th>";
}
echo "<th>age</th>"; mysqli_close($link);
echo "</tr>"; ?>
Code Explanation:
1.The “res” variable stores the data that is returned by the
function mysql_query().
2.Everytime mysqli_fetch_array() is invoked, it returns the next row
from the res() set.
3.The while loop is used to loop through all the rows of the table “data”.
SELECT Query using Object Oriented Method :
<?php while ($row = $res->fetch_array())
$mysqli = new mysqli("localhost", "root", {
"", "Mydb"); echo "<tr>";
if ($mysqli === false) { echo "<td>".$row['Firstname']."</td>";
echo "<td>".$row['Lastname']."</td>";
die("ERROR: " .$mysqli->connect_error);
echo "<td>".$row['Age']."</td>";
} echo "</tr>";
$sql = "SELECT * FROM Data"; }
if ($res = $mysqli->query($sql)) { echo "</table>";
if ($res->num_rows > 0) { $res->free();
echo "<table>"; }
else {
echo "<tr>";
echo "No matching records are found.";
echo }}
"<th>Firstname</th>"; else {
echo echo "ERROR: Could not execute $sql. " .
"<th>Lastname</th>"; $mysqli->error;
echo "<th>Age</th>"; }
echo "</tr>"; $mysqli->close();
?>
SELECT Query using PDO Method :
<?php while ($row = $res->fetch()) {
echo "<tr>";
try { $pdo = new PDO("mysql:host = localhost; dbname=mydb", "root", "");
echo
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); "<td>".$row['Firstname']."</td>";
echo
} "<td>".$row['Lastname']."</td>";
catch (PDOException $e) { echo "<td>".$row['Age']."</td>";
echo "</tr>";
die("ERROR: Could not connect. ".$e->getMessage()); } }
try { $sql = "SELECT * FROM Data"; echo "</table>";
unset($res);
$res = $pdo->query($sql);
}
if ($res->rowCount() > 0) { else {
echo "No matching records are
echo "<table>";
found.";
echo "<tr>"; }}
catch (PDOException $e) {
echo "<th>Firstname</th>";
die("ERROR: Could not able to execute
echo "<th>Lastname</th>"; $sql. " .$e->getMessage());
}
echo "<th>Age</th>";
unset($pdo);
echo "</tr>"; ?>
PHP | MySQL Delete Query
• The DELETE query is used to delete records from a database table.
• It is generally used along with the “Select” statement to delete only
those records that satisfy a specific condition.
• Syntax : The basic syntax of the Delete Query is
<?php
try{
$pdo = new PDO("mysql:host=localhost; dbname=Mydb", "root", "");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e){
die("ERROR: Could not connect. " . $e->getMessage());
}
try{
$sql = "DELETE FROM Data WHERE ID=201";
$pdo->exec($sql);
echo "Record was deleted successfully.";
} catch(PDOException $e){
die("ERROR: Could not able to execute $sql. ". $e->getMessage());
}
unset($pdo);
?>
PHP | MySQL WHERE Clause
• The WHERE Clause is used to filter only those records that are fulfilled by a specific
condition given by the user.
• in other words, the SQL WHERE clause is used to restrict the number of rows affected
by a SELECT, UPDATE or DELETE query.
• Syntax : The basic syntax of the where clause is –
SELECT Column1 , Column2 , …. FROM Table_Name WHERE Condition
Implementation of WHERE Clause :
• To select all the rows where the “Firstname” is “ram”, we will use the following code :
Where Clause using Procedural Method :
<?php while($row = mysqli_fetch_array($res)){
$link = mysqli_connect("localhost", "root", "", echo "<tr>";
"Mydb"); echo "<td>" . $row['Firstname'] . "</td>";
if($link === false){ echo "<td>" . $row['Lastname'] . "</td>";
die("ERROR:connect.".mysqli_connect_error()); echo "<td>" . $row['Age'] . "</td>";
} echo "</tr>";
}
$sql = "SELECT * FROM Data WHERE
Firstname='ram'"; echo "</table>";
mysqli_free_result($res);
if($res = mysqli_query($link, $sql)){
} else{
if(mysqli_num_rows($res) > 0){
echo "No Matching records are found.";
echo "<table>";
}
echo "<tr>"; } else{
echo "<th>Firstname</th>"; echo "ERROR: Could not able to execute $sql. ".
echo "<th>Lastname</th>"; mysqli_error($link);
echo "<th>age</th>"; }
mysqli_close($link);
echo "</tr>";
Code Explanation :
1.The “res” variable stores the data that is returned by the
function mysql_query().
2.Everytime mysqli_fetch_array() is invoked, it returns the next row
from the res() set.
3.The while loop is used to loop through all the rows of the table “data”.
Where Clause using Object Oriented Method :
while($row = $res->fetch_array()){
<?php
echo "<tr>";
$mysqli = new mysqli("localhost", "root", "", "Mydb");
echo "<td>" . $row['Firstname'] . "</td>";
if($mysqli === false){
echo "<td>" . $row['Lastname'] . "</td>";
die("ERROR: Could not connect. ". $mysqli->connect_error);
echo "<td>" . $row['Age'] . "</td>";
}
echo "</tr>";
$sql = "SELECT * FROM Data WHERE Firstname='ram'";
}
if($res = $mysqli->query($sql)){
echo "</table>";
if($res->num_rows > 0){
$res->free();
echo "<table>";
} else{
echo "<tr>";
echo "No matching records are found."; }
echo "<th>Firstname</th>";
} else{
echo "<th>Lastname</th>";
echo "ERROR: Could not execute $sql. ".
echo "<th>Age</th>";
$mysqli->error;
echo "</tr>";
}
•
$mysqli->close();
Where Clause using PDO Method :
<?php
while($row = $res->fetch()){
try{ echo "<tr>";
$pdo = new PDO("mysql:host=localhost; dbname=Mydb", echo "<td>" . $row['Firstname'] . "</td>";
"root", "");
echo "<td>" . $row['Lastname'] . "</td>";
$pdo- echo "<td>" . $row['Age'] . "</td>";
>setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); echo "</tr>";
} catch(PDOException $e){ }
die("ERROR: not connect. ". $e->getMessage()); } echo "</table>";
try{ unset($res);
$sql = "SELECT * FROM Data WHERE Firstname='ram'"; } else{
$res = $pdo->query($sql); echo "No records matching are found."; }
if($res->rowCount() > 0){ } catch(PDOException $e){
echo "<table>"; die("ERROR: Could not able to execute
echo "<tr>"; $sql. " . $e->getMessage());
}
echo "<th>Firstname</th>";
unset($pdo);
echo "<th>Lastname</th>"; ?>
echo "<th>Age</th>";
echo "</tr>";
PHP MySQL UPDATE Query
• The MySQL UPDATE query is used to update existing records in a
table in a MySQL database.
• It can be used to update one or more field at the same time.
• It can be used to specify any condition using the WHERE clause.
• Syntax : The basic syntax of the Update Query is –
Implementation of Where Update Query :
• Let us consider the following table “Data” with four columns ‘ID’,
‘FirstName’, ‘LastName’ and ‘Age’.
<?php
$link = mysqli_connect("localhost", "root", "", "Mydb");