php 警告:mysql_num_rows():提供的参数不是有效的 MySQL 结果资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6059589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource
提问by AdamMc
Possible Duplicate:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result
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
警告:mysql_num_rows():在第 28 行的 ../view-ind-order.php 中提供的参数不是有效的 MySQL 结果资源
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)
该脚本应该检索订单(从列出订单表中所有 order_id 行的页面)、订单内容、订购用户和产品信息。我认为我收到错误的地方是订单中有不止一种产品,但我不太清楚我哪里出错了。(标题有会话启动命令)
<?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');
?>
回答by CristiC
Change $result = @mysql_query ($query);
改变 $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:
您在 oc.price 之后和 prd.products_id 之前遗漏了一个逗号。像这样更改您的查询:
$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
";
回答by Haim Evgi
回答by Spudley
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.
您需要$result
在调用之前检查它是否有效mysql_num_rows()
(即通常紧接在 之后mysql_query()
,否则您可能会收到此错误。
However, if $result
isn't a valid resource, then it means that your query failed, so the problem is earlier than mysql_num_rows()
.
但是,如果$result
不是有效资源,则表示您的查询失败,因此问题早于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.
我注意到你@
用来抑制mysql_query()
. 你为什么做这个?如果您从中收到错误,您需要 (a) 了解它,并且 (b) 修复它。那里的错误意味着您的 SQL 查询有错误 - 您应该先调查此问题,因为这是您真正的问题所在。
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)
您可以mysql_query()
通过使用该mysql_error()
函数找出引发了什么错误。它会告诉您查询有什么问题。(乍一看查询看起来不错,但您很容易拼错字段名称,或者在错误的表前缀中输入字段等;您需要查看错误响应才能确定问题是什么)
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).
顺便说一句,is_numeric
对于输入是否有效,这不是一个很好的检查:它可以为在您的查询中失败的值返回 true(例如科学指数格式的数字、十进制值等)。
回答by jeroen
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.
如果$result
不是有效的 MySQL 结果资源,则意味着查询未返回资源但false
,因此您的查询失败。
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
}
回答by pad31
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.
第一:您是否在 MySQL 查询浏览器等 MySQL 环境中尝试过请求,以了解请求是否正确以及请求是否返回结果。
Second :Can't you use a normal mysql_query function to send the request to your MySQL server ?
第二:您不能使用普通的 mysql_query 函数将请求发送到您的 MySQL 服务器吗?
回答by Headshota
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.
echo
错误以找出导致问题的原因。