How to get the total number of rows changed, deleted, or inserted by the last executed statement using MySQLi in PHP

1 Answer

0 votes
$db_host        = 'localhost';
$db_user        = 'root';
$db_password    = '';
$db_name        = 'allonpage';
  
$con = new mysqli('localhost', $db_user, $db_password, $db_name);
  
if ($con->connect_error) 
    die('Connection Error: ' . $con->server_info);
  
$src = "https://www.ws.com";
$src = $con->real_escape_string($src);
 
$href = "http://www.ws.com/images/logo-wordpress.png";
$href = $con->real_escape_string($href);

// mysqli_stmt::$affected_rows - mysqli_stmt_affected_rows 
// affected_rows - return the number of rows affected by INSERT, UPDATE, or DELETE query
 
if ($con->query("INSERT into test_table (href, src) VALUES ('$href', '$src')")) 
    echo "Row inserted: " . $con->affected_rows;
else
    echo  "Error: " . $con->error; 
 
$con->close();
 
/*
run: 
 
Row inserted: 1 
 
*/

 



answered Jul 11, 2016 by avibootz
...