PHP - Why should I put an IF in mysqli_prepare ()

3

In the example discussed in the PHP Manual , if instructions. I know that if is used for comparisons, what comparison is being made here? Here is an example from 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();
?>
    
asked by anonymous 26.07.2016 / 20:46

1 answer

7
if ($stmt = $mysqli->prepare("SELECT District FROM City WHERE Name=?")) {

This if checks if the query does not have any syntax errors or 'execution' as a constraint violation, if I succeed, it assigns it to $stmt . The absence of it can result in error in% with% with% with% having returned% with% in place of a% object with% of then% error with%     

26.07.2016 / 20:54