I’m making a PHP form that retrieves information from a database based on a serial code entered. I am getting this error. I’ve tried moving stuff around and even calling the $result function inside the if statement instead of referencing the variable.
Error: Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /home/content/98/10339998/html/scripts/stories.php on line 22
Line 20-22:
$result = mysqli_query($con, $query);
if (mysqli_num_rows($result))
Full Code:
<?php
if ( $_POST ) {
$username="*******";
$password="*******";
$con = mysqli_connect("storycodes.db.10339998.hostedresource.com",$username,$password);
if (!$con) {
die('Could not connect: ' . mysqli_error());
}
mysqli_select_db($con, "storycodes");
$code = $_POST['codeInput'];
$code = mysqli_escape_string($con, htmlspecialchars($code)); //May not acually need htmlspecialchars
$query = "SELECT story,video FROM `storycodes` WHERE `code` = $code";
$result = mysqli_query($con, $query);
if (mysqli_num_rows($result)) {
$row = mysqli_fetch_assoc($result);
mysqli_free_result($result);
extract($row);
echo $story . $video;
} else {
echo "No Data Found. Please check your serial code to ensure that you have not incorrectly entered it. If the code is correct please email the website administrator for further assistance";
}
mysqli_close($con);
}
?>
In case your query returns a syntax error, or fails to execute due to any reason, it returns a boolean
FALSE. If you try to pass a boolean tomysqli_num_rows, you will get the warning.In your query, you have missed the quotes around
$code. Just use'$code'instead, and the error should be fixed.