I want to simply display a value fetched via a query.
我想简单地显示通过查询获取的值。
$newquery = "SELECT fullname FROM `users` WHERE user_id=" . $row['usernumber'];
$newresult=mysql_query($newquery);
$newrow = mysql_fetch_row($newresult);
echo "<td>" . $newrow[0]. "</td>";
(Where $row['usernumber']
is fetched from a previous query.)
(从前一个查询中获取$ row ['usernumber']。)
I get this error:
我收到此错误:
Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given in /a/b/viewall.php on line 41
Which I presume, means that my query is failing because of some reason.
我认为,这意味着我的查询由于某种原因而失败。
Here if I echo $newquery;
I get
如果我回复$ newquery;我明白了
SELECT fullname FROM `users` WHERE user_id=3
And if I run this in mysql separately, it returns the fullname
corresponding to user_id=3
properly.
如果我在mysql中单独运行它,它会正确返回与user_id = 3对应的全名。
What could be the problem?
可能是什么问题呢?
5 个解决方案
#1
0
For starters, put some error checking after mysql_query:
对于初学者,在mysql_query之后进行一些错误检查:
$newresult = mysql_query($newquery);
if (!$newresult) {
die('Invalid query: ' . mysql_error());
}
#2
0
Always write PHP code to check the return value of mysql_query()
(or equivalent function in mysqli or PDO). It returns false
if there's an error. That's why you get the error you did, because $newresult was false
-- not a legitimate query resource.
总是编写PHP代码来检查mysql_query()(或mysqli或PDO中的等效函数)的返回值。如果出现错误,则返回false。这就是你得到错误的原因,因为$ newresult是假的 - 不是合法的查询资源。
If mysql_query() returns false, you should report the error message and find out what's wrong.
如果mysql_query()返回false,您应该报告错误消息并找出错误。
$newquery = "SELECT fullname FROM `users` WHERE user_id=" . $row['usernumber'];
$newresult=mysql_query($newquery);
if ($newresult === false) { die(mysql_error()); }
$newrow = mysql_fetch_row($newresult);
For example, other errors are possible instead of syntax errors. Perhaps you don't have the privileges needed to read that table when connecting from PHP, even though you do have the privileges when connecting in a direct query client.
例如,可能存在其他错误而不是语法错误。也许您在从PHP连接时没有读取该表所需的权限,即使您在直接查询客户端中连接时确实具有权限。
#3
0
On my system, it works best to backtick all field/table names and to single-quote variables.
在我的系统上,最好是反击所有字段/表名称和单引号变量。
The addition of mysql_error()
should also help you identify the error.
添加mysql_error()也可以帮助您识别错误。
Note change to query text.
注意更改查询文本。
Try this:
$un = $row['usernumber'];
$newquery = "SELECT `fullname` FROM `users` WHERE `user_id`= '$un'";
$newresult=mysql_query($newquery) or die(mysql_error());
$newrow = mysql_fetch_row($newresult);
echo "<td>" . $newrow[0]. "</td>";
#4
0
It looks likely to be the connection / database selection code which precedes this code block.
它看起来很可能是此代码块之前的连接/数据库选择代码。
As others have mentioned, Please look in to other database driver alternatives (mysqli or PDO)
正如其他人提到的那样,请查看其他数据库驱动程序替代方案(mysqli或PDO)
#5
0
try this
$newquery = sprintf("SELECT fullname FROM `users` WHERE user_id=%d " ,$row['usernumber']);
$result = mysql_query($newquery);
if (!$result )
{
die('Invalid query: ' . mysql_error());
}
else
{
//do something
}
#1
0
For starters, put some error checking after mysql_query:
对于初学者,在mysql_query之后进行一些错误检查:
$newresult = mysql_query($newquery);
if (!$newresult) {
die('Invalid query: ' . mysql_error());
}
#2
0
Always write PHP code to check the return value of mysql_query()
(or equivalent function in mysqli or PDO). It returns false
if there's an error. That's why you get the error you did, because $newresult was false
-- not a legitimate query resource.
总是编写PHP代码来检查mysql_query()(或mysqli或PDO中的等效函数)的返回值。如果出现错误,则返回false。这就是你得到错误的原因,因为$ newresult是假的 - 不是合法的查询资源。
If mysql_query() returns false, you should report the error message and find out what's wrong.
如果mysql_query()返回false,您应该报告错误消息并找出错误。
$newquery = "SELECT fullname FROM `users` WHERE user_id=" . $row['usernumber'];
$newresult=mysql_query($newquery);
if ($newresult === false) { die(mysql_error()); }
$newrow = mysql_fetch_row($newresult);
For example, other errors are possible instead of syntax errors. Perhaps you don't have the privileges needed to read that table when connecting from PHP, even though you do have the privileges when connecting in a direct query client.
例如,可能存在其他错误而不是语法错误。也许您在从PHP连接时没有读取该表所需的权限,即使您在直接查询客户端中连接时确实具有权限。
#3
0
On my system, it works best to backtick all field/table names and to single-quote variables.
在我的系统上,最好是反击所有字段/表名称和单引号变量。
The addition of mysql_error()
should also help you identify the error.
添加mysql_error()也可以帮助您识别错误。
Note change to query text.
注意更改查询文本。
Try this:
$un = $row['usernumber'];
$newquery = "SELECT `fullname` FROM `users` WHERE `user_id`= '$un'";
$newresult=mysql_query($newquery) or die(mysql_error());
$newrow = mysql_fetch_row($newresult);
echo "<td>" . $newrow[0]. "</td>";
#4
0
It looks likely to be the connection / database selection code which precedes this code block.
它看起来很可能是此代码块之前的连接/数据库选择代码。
As others have mentioned, Please look in to other database driver alternatives (mysqli or PDO)
正如其他人提到的那样,请查看其他数据库驱动程序替代方案(mysqli或PDO)
#5
0
try this
$newquery = sprintf("SELECT fullname FROM `users` WHERE user_id=%d " ,$row['usernumber']);
$result = mysql_query($newquery);
if (!$result )
{
die('Invalid query: ' . mysql_error());
}
else
{
//do something
}