Hello,
Got questions 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()
Which of the following examples A-D are a good way of coding and why ?
And, which are a bad way of coding and why ?
Here are my notes.
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 = 'admin@magi.com'; //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 = 'admin@magi.com'; //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 = 'admin@magi.com'; //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 = 'admin@magi.com'; //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);
Looking above, is there any that is unnecessarily going through the IF condition ? (Is pointless). If so, then why ?
Thank you for your helps!