overboard182 overboard182 - 1 year ago 91
MySQL Question

why query is successful but returning 0 rows?

maybe I am overlooking something painfully obvious, but here is my problem. I am selecting information from my database (which I know should return 4 results) and the select runs successfully but when I try to count the number of results I get a null.

I have the following in my php code

$query = "select ORDERS.order_id, PRODUCT.name, PRODUCT_ORDERS.quantity,
ORDERS.order_id = PRODUCT_ORDERS.order_id AND PRODUCT.product_id =
PRODUCT_ORDERS.product_id and ORDERS.customer_id = $customer_id";

$results = mysqli_query($con, $query);

if (mysqli_query($con, $query)) {
echo "Successfully run query: $query";
} else {
echo "Invalid input error: " . $query . "<br>" . mysqli_error($con);

$num = mysqli_num_rows($result);
echo $num; //test to see if i get a number

I have used the same select statement in mysql workbench and it worked and returned 4 results as expected. Is there something specific in PHP that I should be looking out for? Thanks.

Answer Source

Try this

$num = mysqli_num_rows($results);
echo $num; 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download