Hiya,
This questions is on Sql and Php Prepared Statements.
How would you check whether the SQL managed to update a row or not ? Which one of the following lines would you add the IF condition to ?
1.
““mysqli_stmt_execute()
2.
““ mysqli_stmt_affected_rows()
Here are my experiment findings.
A).
““mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);
$conn = mysqli_connect(“localhost”,”root”,””,”buzz”); //mysqli_connect(“server”,”user”,”password”,”db”);
$input_1 = ‘magi’; //username.
$input_2 = ‘[email protected]’; //email.
$input_3 = ‘0’; //id.
$sql = “UPDATE users SET username=?,email=? WHERE id=?”;
$stmt = mysqli_prepare($conn,$sql);
mysqli_stmt_bind_param($stmt,”sss”,$input_1,$input_2,$input_3);
mysqli_stmt_execute($stmt);
echo ‘UPDATED SUCESSFULLY: ‘ .mysqli_stmt_affected_rows($stmt);
mysqli_stmt_close($stmt);
mysqli_close($conn);
B.
““mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);
$conn = mysqli_connect(“localhost”,”root”,””,”buzz”); //mysqli_connect(“server”,”user”,”password”,”db”);
$input_1 = ‘magi’; //username.
$input_2 = ‘[email protected]’; //email.
$input_3 = ‘0’; //id.
$sql = “UPDATE users SET username=?,email=? WHERE id=?”;
if($stmt = mysqli_prepare($conn,$sql))
{
mysqli_stmt_bind_param($stmt,”sss”,$input_1,$input_2,$input_3);
mysqli_stmt_execute($stmt);
echo ‘UPDATED SUCESSFULLY: ‘ .mysqli_stmt_affected_rows($stmt);
}
else
{
echo ‘Mysqli Error: ‘ .mysqli_error();
echo ‘<br>’;
echo ‘Mysqli Error No: ‘ .mysqli_errno();
}
mysqli_stmt_close($stmt);
mysqli_close($conn);
C.
““mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);
$conn = mysqli_connect(“localhost”,”root”,””,”buzz”); //mysqli_connect(“server”,”user”,”password”,”db”);
$input_1 = ‘magi’; //username.
$input_2 = ‘[email protected]’; //email.
$input_3 = ‘0’; //id.
$sql = “UPDATE users SET username=?,email=? WHERE id=?”;
if($stmt = mysqli_prepare($conn,$sql))
{
mysqli_stmt_bind_param($stmt,”sss”,$input_1,$input_2,$input_3);
if(mysqli_stmt_execute($stmt))
{
echo ‘UPDATED SUCESSFULLY: ‘ .mysqli_stmt_affected_rows($stmt);
}
else
{
echo ‘Mysqli Error: ‘ .mysqli_error();
echo ‘<br>’;
echo ‘Mysqli Error No: ‘ .mysqli_errno();
echo ‘<br>’;
die(‘Failed to INSERT!’);
}
}
else
{
echo ‘Mysqli Error: ‘ .mysqli_error();
echo ‘<br>’;
echo ‘Mysqli Error No: ‘ .mysqli_errno();
}
mysqli_stmt_close($stmt);
mysqli_close($conn);
D.
““mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);
$conn = mysqli_connect(“localhost”,”root”,””,”buzz”); //mysqli_connect(“server”,”user”,”password”,”db”);
$input_1 = ‘magi’; //username.
$input_2 = ‘[email protected]’; //email.
$input_3 = ‘0’; //id.
$sql = “UPDATE users SET username=?,email=? WHERE id=?”;
if($stmt = mysqli_prepare($conn,$sql))
{
mysqli_stmt_bind_param($stmt,”sss”,$input_1,$input_2,$input_3);
mysqli_stmt_execute($stmt);
if(mysqli_stmt_affected_rows($stmt))
{
echo ‘UPDATED SUCESSFULLY: ‘ .mysqli_stmt_affected_rows($stmt);
}
else
{
echo ‘Mysqli Error: ‘ .mysqli_error();
echo ‘<br>’;
echo ‘Mysqli Error No: ‘ .mysqli_errno();
echo ‘<br>’;
die(‘Failed to INSERT!’);
}
}
else
{
echo ‘Mysqli Error: ‘ .mysqli_error();
echo ‘<br>’;
echo ‘Mysqli Error No: ‘ .mysqli_errno();
}
mysqli_stmt_close($stmt);
mysqli_close($conn);
They all work fine. Still, does not mean they are proper way of coding. So need your inputs!
Checking above, are there any that are unnecessarily going through the IF condition ? (Is pointless). If so, then why ?
Thank you for your aids!