// Database connection file to declare $connection variable//
require "config.php";
$query="UPDATE student SET class='Four'";
if ($connection->query($query)) {
echo "Records Updated : ";
echo $connection->affected_rows;
}else{
echo $connection->error;
}
Output
Records Updated : 5
This will only count records changed from any other class to class=Four, it will not count records which already have class=Four // Database connection file to declare $connection variable//
require "config.php";
$query="DELETE FROM student WHERE class='Four'";
$connection->query($query);
echo " Records Deleted = ".$connection->affected_rows;
Output is here
Records Deleted = 9
$query="INSERT INTO student (id,name,class,mark,sex) values(100,'John','Five','80','Male')";
if($connection->query($query)){
echo "
No of records inserted : ".$connection->affected_rows;
}
Output is here
No of records inserted : 1
$query="SELECT * FROM student WHERE class='Four'";
$connection->query($query);
echo " Records returned = ".$connection->affected_rows;
Output is Records returned = 9
$query="SELECT * FROM student WHERE class='Four' LIMIT 2,4";
$connection->query($query);
echo " Records returned = ".$connection->affected_rows;
Records returned = 4
Object oriented style with parameters
<?Php
require "config.php";// Database connection file.
$class='Three';
$mark=66;
$id=5;
$query="UPDATE student SET class=?,mark=? WHERE id=?";
$stmt = $connection->prepare($query);
if ($stmt) {
$stmt->bind_param('sii', $class, $mark, $id);
$stmt->execute();
echo "Record Updated:";
echo $stmt->affected_rows;
}else{
echo $connection->error;
}
?>
Procedural style
$class='Three';
$mark=68;
$id=5;
$query="UPDATE student SET class=?,mark=? WHERE id=?";
if ($stmt = mysqli_prepare($connection,$query)){
mysqli_stmt_bind_param($stmt, "sii", $class, $mark, $id);
mysqli_stmt_execute($stmt);
echo "Record Updated:";
echo mysqli_affected_rows($connection);
}else{
echo mysqli_error($connection);
}
By using mysql_affected_rows we can get number of records changed / updated / deleted /inserted or selected after using SELECT, UPDATE, REPLACE, DELETE and INSERT query .
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.