简体   繁体   English

MySQL查询有效,但在PHP中不起作用

[英]MySQL Query works but not within PHP

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; 如果我在这里echo $newquery; I get 我懂了

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=3fullname

What could be the problem? 可能是什么问题呢?

For starters, put some error checking after mysql_query: 对于初学者,在mysql_query之后进行一些错误检查:

$newresult = mysql_query($newquery);
if (!$newresult) {
   die('Invalid query: ' . mysql_error());
}

Always write PHP code to check the return value of mysql_query() (or equivalent function in mysqli or PDO). 始终编​​写PHP代码以检查mysql_query()的返回值(或mysqli或PDO中的等效函数)。 It returns false if there's an error. 如果有错误,它将返回false That's why you get the error you did, because $newresult was false -- not a legitimate query resource. 这就是为什么您得到错误的原因,因为$ newresult是false -不是合法的查询资源。

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连接时,即使您在直接查询客户端中进行连接时,也可能没有读取该表所需的特权。

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>";

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)

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM