Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php mysql\u查询返回值为0_Php_Mysql - Fatal编程技术网

Php mysql\u查询返回值为0

Php mysql\u查询返回值为0,php,mysql,Php,Mysql,我有一个问题: $img_id = mysql_query("SELECT img_id FROM img_path where ul_id = $ul_id "); $img_id = $img_id[0]; $img\u id最后输出为0,我希望为img\u id存储在表img\u path中的值 我已经检查了数据库连接,它们很好。有什么帮助吗?请尝试使用此代码。你需要去拿唱片 $img_id = mysql_query("SELECT img_id FROM img_path

我有一个问题:

$img_id = mysql_query("SELECT img_id FROM img_path where ul_id = $ul_id ");     
$img_id = $img_id[0];
$img\u id
最后输出为0,我希望为
img\u id
存储在表
img\u path
中的值


我已经检查了数据库连接,它们很好。有什么帮助吗?

请尝试使用此代码。你需要去拿唱片

$img_id = mysql_query("SELECT img_id FROM img_path where ul_id = $ul_id "); 

$imgID = mysql_fetch_array($img_id);

$img_id1 = $imgID['img_id'];
试试这个:

$ul_id = mysql_real_escape_string($ul_id);
$img_id =mysql_query("SELECT img_id FROM img_path where ul_id = '".$ul_id."' "); 
$row = mysql_fetch_array($img_id);
$img = $row['img_id'];
或者如果你想找一排

然后用这个:

$ul_id = mysql_real_escape_string($ul_id);
$img_id =mysql_query("SELECT img_id FROM img_path where ul_id = '".$ul_id."' "); 
$row = mysql_fetch_row($img_id);
$img = $row[0];

  • 考虑更改为MYSQLI或PDO,因为mysql已被弃用

  • 考虑转义变量


您的
$img\u id
包含由返回的

要获取查询结果,您必须使用mysql_fetch_*函数之一获取它,即

此外,出于安全原因,您应该始终转义传递给查询的变量

但正如不推荐的那样,您最好使用或

无论如何,mysql就是这样做的:

$ul_id = mysql_real_escape_string($ul_id);
$img_id = mysql_query("SELECT img_id FROM img_path where ul_id = $ul_id ");
$row = mysql_fetch_array($img_id)
$img_id = $row['img_id']; // access through associative indices or
// $img_id = $row[0]; // access through number indices

您需要从
$img\u id
中的查询对象获取结果。您有一个查询对象不是来自查询的结果。请根据您的解释为我提供正确的代码。mysql\u*函数已过时且不推荐使用。请看一看和。在您的情况下,查询结果应该在使用mysql_fetch_*becode之前获取。