lundi 30 septembre 2019

MySQL/PHP else if statement with affected rows

I am trying to let to check the a table if there exists a value and if there exists a value it should output as JSON "one" and if it doesn't exist it should output "two". My thought is that With my SELECT EXIST Statement it should only return a row if there exists the value in the table but for some reason it always outputs a row.

Here is the code:

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
if ($result = mysqli_query($conn, $query)) {

    $newArr = array();
    $value = mysqli_fetch_object($result);
    $newArr[] = (bool) $value->item_exists;

    echo json_encode($newArr); // get all products in json format.    
}
*/


/* part where output is just 1 or 0*/
$query = "SELECT EXISTS(SELECT * FROM wp_woocommerce_order_items WHERE order_id = $sdata)";
$myArray = array();
if ($result = mysqli_query($conn, $query)) {

        while($row = $result->fetch_row()) {
            $myArray[] = $row;
    }
 // 2nd operation checkif order available
 if ($conn->affected_rows == 1) {
        echo json_encode($one);
    } else {

//Success and return new id
        echo json_encode($two);
    }    
//end 2nd operation


    echo json_encode($myArray);
}$result->close();


echo json_encode($newArr); // get all products in json format.    
}


$conn->close();
?>

Aucun commentaire:

Enregistrer un commentaire