I am facing this error "trying to get property 'num_rows' of non-object".
My code is:
$query = "SELECT * FROM table_name WHERE column = 'value'";
$sql = $conn->prepare($query);
$result = $sql->get_result();
if ($result->num_rows > 0) {
$result = $result->fetch_assoc();
}
Mistake!
You are trying to use the property 'num_rows' while $result is not an object here
Solutions:
Execute query before checking row count
$sql->execute();
Your code can be like:
// Query
$query = "SELECT * FROM table_name WHERE column = 'value'";
// Preparation
$sql = $conn->prepare($query);
//Execution
$sql->execute();
// Get result
$result = $sql->get_result();
// Check number of rows
if ($result->num_rows > 0) {
$result = $result->fetch_assoc();
// your php code here
}
// Query
$query = "SELECT * FROM table_name WHERE column = 'value'";
$result = mysqli_query($conn, $query);
// Check number of rows
if ($result->num_rows > 0) {
$result = $result->fetch_assoc();
print_r($result);
// your php code here
}
If you still have a question about this, submit it in our Q&A community - Ask Question