通过PHP回复MySQL数据库中的用户[重复]

时间:2022-09-22 16:04:22

Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select

可能重复:mysql_fetch_array()期望参数1是资源,在select中给出boolean

I have the following code

我有以下代码

$result = mysql_query("SELECT * FROM members WHERE group = '$groupid'");

while($row = mysql_fetch_array($result))
{
  echo $row['name'];
}

which I want to echo all users who are in a certain group, but I get the error "Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\Program Files\wamp\www\state_ecotracker\group.php on line 11"

我希望回应某个组中的所有用户,但是我收到错误“警告:mysql_fetch_array()期望参数1是资源,布尔值在C:\ Program Files \ wamp \ www \ state_ecotracker \ group.php中给出在第11行“

I do not know what to do.

我不知道该怎么办。

4 个解决方案

#1


3  

Your PHP code is correct.

您的PHP代码是正确的。

GROUP is a reserved word in MySQL as it corresponds to the GROUP BY clause of SELECT queries.

GROUP是MySQL中的保留字,因为它对应于SELECT查询的GROUP BY子句。

You shouldn't use it as a column name. Change your table to call group something else.

您不应将其用作列名。更改您的表格以调用其他内容。

If you can't do this for some slightly absurd reason (I don't know your boss), you'll have to always enclose that column name in backticks in every SQL query that uses it.

如果由于一些有点荒谬的原因(我不认识你的老板)你不能这样做,那么你必须在每个使用它的SQL查询中将该列名一直包含在反引号中。

$result = mysql_query("SELECT * FROM members WHERE `group` = '$groupid'");

#2


0  

Change

$result = mysql_query("SELECT * FROM members WHERE group = '$groupid'");`

to

$result = mysql_query("SELECT * FROM members WHERE group = '$groupid'") or die(mysql_error());

You can then see what error message MySQL is throwing you, once you fix that your query should work fine.

然后,一旦你修复了你的查询应该正常工作,你就可以看到MySQL给你带来了什么错误信息。

It's also worth noting that if you really want to use those MySQL PHP functions and not something like PDO then you should ideally use mysql_fetch_assoc and not mysql_fetch_array.

值得注意的是,如果你真的想要使用那些MySQL PHP函数而不是像PDO那样的东西,那么理想情况下你应该使用mysql_fetch_assoc而不是mysql_fetch_array。

#3


0  

While using mysql reserved words as field name, enclose them in `:

使用mysql保留字作为字段名时,将它们包含在`:

$result = mysql_query("SELECT * FROM members WHERE `group` = '$groupid'");

Or rename the field name.

或者重命名字段名称。

#4


0  

First you should sanitise your input:

首先,您应该清理您的输入:

$groupId = (int)$groupId; // cast to integer (I suppose because of the Id)
$result = mysql_query('SELECT * FROM members WHERE `group` = ' . $groupId); 
// use backticks because GROUP is a reserved word.

while ($row = mysql_fetch_array($result)) {
  echo $row['name'];
}

You can use mysql_error() to check what is causing an error with the query.

您可以使用mysql_error()来检查导致查询错误的原因。

#1


3  

Your PHP code is correct.

您的PHP代码是正确的。

GROUP is a reserved word in MySQL as it corresponds to the GROUP BY clause of SELECT queries.

GROUP是MySQL中的保留字,因为它对应于SELECT查询的GROUP BY子句。

You shouldn't use it as a column name. Change your table to call group something else.

您不应将其用作列名。更改您的表格以调用其他内容。

If you can't do this for some slightly absurd reason (I don't know your boss), you'll have to always enclose that column name in backticks in every SQL query that uses it.

如果由于一些有点荒谬的原因(我不认识你的老板)你不能这样做,那么你必须在每个使用它的SQL查询中将该列名一直包含在反引号中。

$result = mysql_query("SELECT * FROM members WHERE `group` = '$groupid'");

#2


0  

Change

$result = mysql_query("SELECT * FROM members WHERE group = '$groupid'");`

to

$result = mysql_query("SELECT * FROM members WHERE group = '$groupid'") or die(mysql_error());

You can then see what error message MySQL is throwing you, once you fix that your query should work fine.

然后,一旦你修复了你的查询应该正常工作,你就可以看到MySQL给你带来了什么错误信息。

It's also worth noting that if you really want to use those MySQL PHP functions and not something like PDO then you should ideally use mysql_fetch_assoc and not mysql_fetch_array.

值得注意的是,如果你真的想要使用那些MySQL PHP函数而不是像PDO那样的东西,那么理想情况下你应该使用mysql_fetch_assoc而不是mysql_fetch_array。

#3


0  

While using mysql reserved words as field name, enclose them in `:

使用mysql保留字作为字段名时,将它们包含在`:

$result = mysql_query("SELECT * FROM members WHERE `group` = '$groupid'");

Or rename the field name.

或者重命名字段名称。

#4


0  

First you should sanitise your input:

首先,您应该清理您的输入:

$groupId = (int)$groupId; // cast to integer (I suppose because of the Id)
$result = mysql_query('SELECT * FROM members WHERE `group` = ' . $groupId); 
// use backticks because GROUP is a reserved word.

while ($row = mysql_fetch_array($result)) {
  echo $row['name'];
}

You can use mysql_error() to check what is causing an error with the query.

您可以使用mysql_error()来检查导致查询错误的原因。