'Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource [duplicate]
I am getting the following error message on the code below (which is at the end of the query):
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in ../view-ind-order.php on line 28
This script is supposed to retrieve the order (from a page which lists all of the order_id rows from the orders table), the contents of the order, the user who ordered and the product information. I think where I'm getting the error is where there is more than one product within the order but I can't quite see where I'm going wrong. (the header has a session start command)
<?php
$page_title = 'Order';
include ('./includes/header.html');
if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) )
{
$id = $_GET['id'];
} elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) )
{
$id = $_POST['id'];
} else {
echo 'This page has been accessed in error';
include ('./includes/header.html');
exit();
}
require_once ('mydatabase.php');
$query = "SELECT us.users_id, us.users_first_name, us.users_surname, us.users_business,
ord.order_id, ord.users_id, ord.total, ord.order_date,
oc.oc_id, oc.order_id, oc.products_id, oc.quantity, oc.price
prd.products_id, prd.products_name, prd.price
FROM users AS us, orders AS ord, order_contents AS oc, products AS prd
WHERE ord.order_id=$id
AND us.users_id = ord.users_id
AND ord.order_id = oc.order_id
AND oc.products_id = prd.products_id
";
$result = @mysql_query ($query);
if (mysql_num_rows($result) == 1) {
$row = mysql_fetch_array ($result, MYSQL_NUM);
echo '
<table>
<tr>
<td><strong>Name:</strong></td>
<td>' . $row[1] . ' ' . $row[2] . '</td>
</tr>
<tr>
<td><strong>Business Name</strong></td>
<td>' . $row[4] . '</td>
</tr>
<tr>
<td><strong>Total:</strong></td>
<td>' . $row[7] . '</td>
</tr>
<tr>
<td><strong>Quantity</strong></td>
<td>' . $row[12] . '</td>
</tr>
<tr>
<td><strong>Product:</strong></td>
<td>' . $row[15] . '</td>
</tr>
<tr>
<td><strong>Price:</strong></td>
<td>' . $row[13] . '</td>
</tr>
</table>
';
} else {
echo '<h1 id="mainhead">Page Error</h1>
<p class="error">This page has been accessed in error.</p><p><br /><br /></p>';
}
mysql_close();
include ('./includes/footer.html');
?>
Solution 1:[1]
Change $result = @mysql_query ($query);
with
$result = mysql_query ($query) or die(mysql_error());
and see if you have any errors.
EDIT:
You missed a comma after oc.price and before prd.products_id. Change your query like this:
$query = "SELECT us.users_id, us.users_first_name, us.users_surname, us.users_business,
ord.order_id, ord.users_id, ord.total, ord.order_date,
oc.oc_id, oc.order_id, oc.products_id, oc.quantity, oc.price/*here*/,/**/
prd.products_id, prd.products_name, prd.price
FROM users AS us, orders AS ord, order_contents AS oc, products AS prd
WHERE ord.order_id=$id
AND us.users_id = ord.users_id
AND ord.order_id = oc.order_id
AND oc.products_id = prd.products_id
";
Solution 2:[2]
you probably have an error in your sql,
show it like:
if (!mysql_query($query, $link)) {
echo mysql_errno($link) . ": " . mysql_error($link) . "\n";
}
Solution 3:[3]
If $result
is not a valid MySQL result resource, that means that the query did not return a resource but false
, so your query failed.
You should not suppress the error message or at least check for the result of the query to not be false before you proceed using the results:
if (!$result)
{
// error
}
else
{
// use results
}
Solution 4:[4]
You need to check that $result
is valid before calling mysql_num_rows()
(ie usually immediately after mysql_query()
, otherwise you can get this error.
However, if $result
isn't a valid resource, then it means that your query failed, so the problem is earlier than mysql_num_rows()
.
I note that you're using @
to supress any errors thrown by mysql_query()
. Why are you doing this? If you're getting an error from it, you need to (a) know about it, and (b) fix it. An error there implies that your SQL query has bugs - you should investigate this before anything else, as this is where your real problem lies.
You can find out what error was thrown by mysql_query()
by using the mysql_error()
function. It will tell you what the problem is with the query. (at first glance the query looks okay, but you could easily have mis-spelled a field name, or got a field in the wrong table prefix, etc; you'd need to see the error response to know for sure what the problem is)
By the way, is_numeric
is not a good check for whether the input is valid or not: it can return true for values which will fail in your query (eg numbers in scientific exponential format, decimal values, etc).
Solution 5:[5]
First : Have you tried the request in a MySQL environement like MySQL Query Browser to know if the request is right and if the request return a result.
Second : Can't you use a normal mysql_query function to send the request to your MySQL server ?
Solution 6:[6]
apparently a query is causing an error:
$result = mysql_query ($query) or trigger_error(mysql_error(),E_USER_ERROR);
echo
the error to find out what is causing the problem.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Paul Fleming |
Solution 2 | Haim Evgi |
Solution 3 | jeroen |
Solution 4 | |
Solution 5 | Paul Fleming |
Solution 6 |