Jump to: navigation, search

PHP MySQLi Update Records and Return affected rows

From w3cyberlearnings

Contents

PHP MySQLi Update Record Use Binding

Use binding to update record.

Syntax Update

$name = 'Paul'; // to be update
$query = "UPDATE student SET age=12 WHERE name=?";
$conn = $db->prepare($query);
$conn->bind_param("s", $name);
$conn->execute();

Syntax get affected row

$row_affected = $db->affected_rows;

Example 1

Record only updates once when it runs.

<?php

define('HOST', 'localhost');
define('USER', 'user2000');
define('PASS', 'password2000');
define('DBNAME', 'w3cyberlearning');

$db = new mysqli(HOST, USER, PASS, DBNAME);

$name = 'Paul'; // to be update

if ($db->connect_errno) {
	echo "Failed to connect to MySQL: (" . $db->connect_errno . ") "
	. $db->connect_error;
}

$query = "UPDATE student SET age=12 WHERE name=?";
$conn = $db->prepare($query);
$conn->bind_param("s", $name);
$conn->execute();

$row_affected = $db->affected_rows;
if ($row_affected) {
	echo $row_affected;
} else {
	echo 'no update';
}

$db->close();
?>

Output

1


<sidebar>

    • PHP MySQL mysqli|PHP MySQL mysqli
    • PHP MySQLi Connect|PHP MySQLi Connect
    • PHP MySQLi Create Table|PHP MySQLi Create Table
    • PHP MySQLi Insert Records Using Binding|PHP MySQLi Insert Records use Binding
    • PHP MySQLi Fetch Records as an associative array|PHP MySQLi Fetch Records as an associative array
    • PHP MySQLi Fetch All Records|PHP MySQLi Fetch All Records
    • PHP MySQLi Fetch Records And Bind to variables|PHP MySQLi Fetch Records And Bind to variables
    • PHP MySQLi Fetch as object|PHP MySQLi Fetch as object
    • PHP MySQLi Fetch Records Use binding|PHP MySQLi Fetch Records Use binding
    • PHP MySQLi Update Records and Return affected rows|PHP MySQLi Update Records and Return affected rows
    • PHP MySQLi Delete Records Use binding|PHP MySQLi Delete Records Use binding

</sidebar>

Navigation
Web
SQL
MISC
References