I have a PHP file which gets JSON formatted data via a POST request.
I'm using prepared statements and everything works fine until calling mysqli_fetch_assoc
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\LoanAppTest\login.php on line 31
This only happens when I declare mysqli_stmt_execute($stmt);
, if I comment it out then it would get me the result that I want however I need to have mysqli_stmt_execute($stmt);
so that $count = mysqli_stmt_num_rows($stmt);
could work. This is my code:
if($jsonobj == null){
header("Location: http://localhost/dashboard");
} else {
include "config.php";
$obj = json_decode($jsonobj, true);
$clientID = (int)$obj['prop_ClientID'];
$query = "SELECT ClientID, FirstName, LastName, MiddleName, ContactNo FROM tblClient WHERE ClientID = ?";
// Initialize prepared statement
$stmt = mysqli_stmt_init($con);
if (!mysqli_stmt_prepare($stmt, $query)) {
echo "SQL Statmenet Failed.";
} else {
// Bind parameters and execute statement
mysqli_stmt_bind_param($stmt, "i", $clientID);
mysqli_stmt_execute($stmt);
// Store result and count for validation (check if ClientID exists)
mysqli_stmt_store_result($stmt);
$count = mysqli_stmt_num_rows($stmt);
if ($count == 0) {
echo "No Client ID in DB";
} else {
$result = mysqli_stmt_get_result($stmt);
$row = mysqli_fetch_assoc($result); // This part returns the error
print_r($row);
}
}
}