3
In the example in Manual PHP was made use of the if before the execution of the instructions. I know that the if is used for comparisons, what comparison is being made here? Follow the example of the website below:
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$city = "Amersfoort";
/* create a prepared statement */
if ($stmt = $mysqli->prepare("SELECT District FROM City WHERE Name=?")) {
/* bind parameters for markers */
$stmt->bind_param("s", $city);
/* execute query */
$stmt->execute();
/* bind result variables */
$stmt->bind_result($district);
/* fetch value */
$stmt->fetch();
printf("%s is in district %s\n", $city, $district);
/* close statement */
$stmt->close();
}
/* close connection */
$mysqli->close();
?>
The first
if
, if the functionmysqli_connect_errno
return1
(amounts totrue
), means there’s been an error and the code will stop right there.– William Novak
Yes, but I mean the if of the question, mysqli_prepare
– Ronan Silva
The second is pretty much the same. If it worked, execute the commands. Otherwise, just close the connection. Pig tutorials. If you are not to do anything with the error, or use the if. in a real application you will stop and log the error to be able to fix.
– Bacco
$stmt
is a variable that is being created at the time of comparison, the prepare will bring a result boleanus, what the same condition of the firstif
.– William Novak
As well as "worked out", check if the query is valid?
– Ronan Silva
If the "prepare" was successful, the "inside" of the if runs. If the query contains an error, or any other problem occurs, the $stmt expression will be false, so only close will be executed.
– Bacco