在PHP / MySQL中只获取一行[重复]

时间:2021-09-06 22:19:45

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

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

simple question here.

这里简单的问题。

I have a SELECT query

我有一个SELECT查询

SELECT FROM friendzone WHERE ID = '$editID'"

I am sure this is going to give me only one row as result, cause ID's can't be duplicate in my DB.

我确信这只会给我一行,因为我的数据库中的ID不能重复。

How can I access the column values?

如何访问列值?

$row = mysql_fetch_array($query);

I think this is useless since I don't have to make any array.. I have only one row!

我认为这没用,因为我不需要制作任何数组..我只有一行!

If I don't put it into a While cicle, and try to do e.g.

如果我没有将它放入while cicle中,并尝试例如

.$row['ID'].

I get:

我明白了:

mysql_fetch_array() expects parameter 1 to be resource, boolean given

Thanks in advance everyone.

在此先感谢大家。

3 个解决方案

#1


9  

Please, don't use mysql_* functions in new code. They are no longer maintained and the deprecation process has begun on it. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

请不要在新代码中使用mysql_ *函数。它们不再被维护,并且已经开始弃用。看到红色的盒子?转而学习预备语句,并使用PDO或MySQLi - 本文将帮助您确定哪些。如果您选择PDO,这是一个很好的教程。

Try with:

试试:

$query = mysql_query("SELECT * FROM friendzone WHERE ID = '$editID'");
$row = mysql_fetch_array($query);

print_r($row);

MySQLi code:

MySQLi代码:

$conn = new mysqli('host', 'username', 'password', 'database');
$stmt = $conn->prepare("SELECT * FROM friendzone WHERE ID = ?");
$stmt->bind_param("s", $editID);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();

print_r($row);

#2


2  

Probably your $query is equal false because something went wrong, try mysql_error() to see whats wrong.

可能你的$查询是错误的,因为出了问题,试试mysql_error()看错了什么。

And 2 small advices:

还有2个小建议:

  1. would be better to use PDO od mysqli as mysql_* functions are deprecated.

    最好使用PDO od mysqli,因为不推荐使用mysql_ *函数。

  2. use at least mysql_real_escape_string() to escape the value before putting it into SQL string

    在将值放入SQL字符串之前至少使用mysql_real_escape_string()来转义该值

#3


0  

Since I don't know what columns your trying to select the general syntax for select is

由于我不知道您尝试选择哪些列,因此选择的一般语法是

"SELECT column1, column2, column3 FROM friendzone WHERE ID ='$editID'"

Or if you want to select all columns just type

或者,如果要选择所有列,只需键入

"SELECT * FROM friendzone WHERE ID = '$editID'"

#1


9  

Please, don't use mysql_* functions in new code. They are no longer maintained and the deprecation process has begun on it. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

请不要在新代码中使用mysql_ *函数。它们不再被维护,并且已经开始弃用。看到红色的盒子?转而学习预备语句,并使用PDO或MySQLi - 本文将帮助您确定哪些。如果您选择PDO,这是一个很好的教程。

Try with:

试试:

$query = mysql_query("SELECT * FROM friendzone WHERE ID = '$editID'");
$row = mysql_fetch_array($query);

print_r($row);

MySQLi code:

MySQLi代码:

$conn = new mysqli('host', 'username', 'password', 'database');
$stmt = $conn->prepare("SELECT * FROM friendzone WHERE ID = ?");
$stmt->bind_param("s", $editID);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();

print_r($row);

#2


2  

Probably your $query is equal false because something went wrong, try mysql_error() to see whats wrong.

可能你的$查询是错误的,因为出了问题,试试mysql_error()看错了什么。

And 2 small advices:

还有2个小建议:

  1. would be better to use PDO od mysqli as mysql_* functions are deprecated.

    最好使用PDO od mysqli,因为不推荐使用mysql_ *函数。

  2. use at least mysql_real_escape_string() to escape the value before putting it into SQL string

    在将值放入SQL字符串之前至少使用mysql_real_escape_string()来转义该值

#3


0  

Since I don't know what columns your trying to select the general syntax for select is

由于我不知道您尝试选择哪些列,因此选择的一般语法是

"SELECT column1, column2, column3 FROM friendzone WHERE ID ='$editID'"

Or if you want to select all columns just type

或者,如果要选择所有列,只需键入

"SELECT * FROM friendzone WHERE ID = '$editID'"