Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.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/logging/2.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 fetch\u array()需要参数1_Php_Mysql_Warnings - Fatal编程技术网

PHP警告:mysql\u fetch\u array()需要参数1

PHP警告:mysql\u fetch\u array()需要参数1,php,mysql,warnings,Php,Mysql,Warnings,可能重复: 我正在创建用户配置文件,发现了一个问题。当我打开url/链接时,我得到警告: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in *localhost/user_profiles/user_list.php* on line 9. 代码是: <?php function fetch_users(){ $result = mysql_q

可能重复:

我正在创建用户配置文件,发现了一个问题。当我打开url/链接时,我得到警告:

Warning: mysql_fetch_array() expects parameter 1 to be resource, 
boolean given in *localhost/user_profiles/user_list.php* on line 9. 
代码是:

<?php

    function fetch_users(){

       $result = mysql_query(
         'SELECT `user_id` AS `id`, `user_username` AS `username` FROM `login`');

       $users = array();

       while (($row = mysql_fetch_assoc($result)) !== false){ 
          $users[] = $row;
       }

       return $users;
    }
?>

正如其他人所建议的,您的查询没有成功执行。请尝试使用以下代码查明错误:

$result = mysql_query(
     'SELECT `user_id` AS `id`, `user_username` AS `username` FROM `login`') or die (mysql_error());

请注意,
或die()
段可以放在PHP中的许多代码上,以调试代码并准确找出问题发生的位置。出于同样的原因,
mysql\u error()
函数也很好。

我可能会补充一点,在生产系统中死于数据库错误不是一个好的做法,应该更优雅地捕获和处理它们。但作为一种立即发现问题的方法,这将很好地工作。NVM,我修复了它。注意到我有一个问题。非常感谢@halfer我同意这不应该出现在代码的最终版本中,但出于调试目的,我发现它要快得多,除非您已经有了错误处理设置。@carl没问题,我很高兴您能让它工作起来!:)
mysql\u query
返回了
boolean FALSE
,这意味着出现了错误。不要使用my\u sql函数。使用PDO或MySQLi。my_sql函数已贬值。如果您选择PDO,这里有一个很好的教程,您可以将布尔值放入变量中,并将其传递给需要资源的函数,但您不能这样做。建议阅读有关如何使用mysql_fetch_assoc和mysql_fetch_数组的文档。