php mySQL 查询返回资源 ID #5
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19142711/
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
mySQL query returning Resource id #5
提问by Pau
I've searched about possible solutions to this. and I've tried looping as well as trying the variants of mysql_fetch_assoc and mysql_fetch_array but I'm still getting the Resource id #5: 0: error.
我已经搜索了可能的解决方案。我尝试过循环以及尝试 mysql_fetch_assoc 和 mysql_fetch_array 的变体,但我仍然得到资源 ID #5: 0: 错误。
Here's my code which I think generates the error.
这是我认为会产生错误的代码。
<?php
mysqlc();
$email = GetSQLValueString($_SESSION['user'], "text");
$query = sprintf("SELECT * FROM newmember WHERE email = %s",$email);
$res = mysql_query($query) or die('Query failed: ' . mysql_error() . "<br />\n$sql");
$row = mysql_fetch_assoc($res);
?>
回答by kuldeep raj
Please use this one :
请使用这个:
while($row = mysql_fetch_object($res)){
//do somthing
echo $row->name;
}
I am already use this one in our code
我已经在我们的代码中使用了这个
回答by Jacob S
"Resource id #5: 0" is not an error. It means that you tried to echo $res
instead of trying to use the $row
variable, such as $row[column] for fetch_assoc, $row[0] for fetch_row, either/both for fetch_array.
“Resource id #5: 0”不是错误。这意味着您尝试echo $res
而不是尝试使用$row
变量,例如 $row[column] 用于 fetch_assoc,$row[0] 用于 fetch_row,或者/两者都用于 fetch_array。
The other answers explain the use of mysql_fetch_*
其他答案解释了 mysql_fetch_* 的使用
Also, mysql_* is deprecated. You should use mysqli_* or PDO functions instead.
此外,不推荐使用 mysql_*。您应该改用 mysqli_* 或 PDO 函数。
回答by David Hackro
you need to use mysql_fetch_row
你需要使用mysql_fetch_row
<?php
$resultado = mysql_query("SELECT id, email FROM people WHERE id = '42'");
if (!$resultado) {
echo 'No se pudo ejecutar la consulta: ' . mysql_error();
exit;
}
$fila = mysql_fetch_row($resultado);
echo $fila[0]; // 42
echo $fila[1]; // el valor de email
?>