Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\web\xampp\htdocs\www\core\functions\users.php on line 12

 function user_data($user_id) { $data = array(); $user_id = (int) $user_id; $func_num_args = func_num_args(); $func_get_args = func_get_args(); if ($func_num_args > 1) { unset($func_get_args[0]); $fields = '`' . implode('`, `', $func_get_args) . '`'; $data = mysql_fetch_assoc(mysql_query("SELECT $fields FROM `users` WHERE `user_id` = $user_id")); print_r($data); die(); return $data; } } 

Reported as a duplicate by PashaPash members , cyadvert , aleksandr barakin , Aries , Regent 9 Oct '15 at 19:33 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

    3 answers 3

    The error says "Warning: mysql_fetch_assoc () expects parameter 1 to be resource, boolean given" - "Expected resouce, boolean arrived", it means that mysql_query returned boolean. So the request is an error. Rewrite the lines:

     $query = mysql_query("SELECT $fields FROM `users` WHERE `user_id` = $user_id"); if(!$query) return false; $data = mysql_fetch_assoc($query); 
    • This option has earned, but does not display a list in an array from a database like this on the video youtube.com/watch?v=yX1EushqcIw&feature=relmfu 3 minutes 20 seconds! - LLIAKAJI
    • here the second line only handles the error returned from the mysql_query function. You probably have an error either in connecting to the database or in the request. - MuFF

    So do not quite right. Since in this case, you can not correctly handle errors in the request, apparently, which you have present.

    You should put the result [mysql_query][1] in a variable. Then check the value if FALSE - output an error (for example, through mysql_error ). If not FALSE - continue to run the application as intended.

    And it would be worthwhile to think about the fact that there is a search .

    UPD

    Try to display the query itself and execute it in the SQL manager. You may have, for example, unnecessary or missing fields.

    Use this code:

     $res = mysql_query("SELECT $fields FROM `users` WHERE `user_id` = '$user_id'") or die(mysql_error()); 

    Check that the users table contains the fields: user_id , user_name , password , first_name , last_name ... etc

    Well, I've kind of made an epic mistake. While I called it within php. I really thought about the field of knowledge. That 'fix' became a problem later in the tutorials. : D

      This error occurs when a query to the database passed in the function parameter mysql_fetch_assoc returned failure (FALSE), instead of a set of records from the database or success (when modifying a data set, for example), TRUE. The request itself looks a little suspicious. This is probably because I do not know the structure of your table. And this block of code is generally terrible:

        unset($func_get_args[0]); $fields = '`' . implode('`, `', $func_get_args) . '`'; // здесь вы, не окажется запятых между полями...скорее, потому и ошибка. И вообще, возьмите к примеру, первое поле массива. Оно будет такого вида в миссиве **'`field'**, потому что это **'`, `'** не всегда будет верно... $data = mysql_fetch_assoc(mysql_query("SELECT $fields FROM `users` WHERE `user_id` = $user_id")); 

      One thing only:

        $fields = '`' . implode('`, `', $func_get_args) . '`'; 

      How much sadness brought me ...


      Try replacing this:

       unset($func_get_args[0]); $fields = implode(',', $func_get_args); // бьем поля по запятым, вместо вашего ужаса. $fields = mysql_real_escape_string($func_get_args[0]); // подготавливаем к запросу. $data = mysql_fetch_assoc(mysql_query("SELECT $fields FROM `users` WHERE `user_id` = $user_id")); // запросим 

      In general, it is better not to create your own hemorrhoids with these fields, because you can select all the fields, it will work almost as fast as your implode () ...

      • Firstly, this is not my horror, but from the video lesson, secondly, your example also does not work, the same mistake ( - LLIAKAJI
      • @LLIAKAJI, well, an error in the request can, who knows =) Write your code, and do not copy-paste, so that there are no such problems, well say ... - Salivan
      • there were no mistakes before, besides, who did not start from copying, they first learn and then write, on the contrary, it will not work, besides, I don’t have the same mistake on the comments to the video of powerful people, but they can’t get it wrong - LLIAKAJI