0

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

I'm creating user profile and I noticed a problem. When I open the url/link I get the warning:

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

the code is:

<?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;
    }
?>
Community
  • 1
  • 1
carl
  • 1
  • 2
  • 3
    Dont use my_sql functions. Use PDO or MySQLi. my_sql functions are depreciated. If you choose PDO, here is a good tutorial http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers – Goaler444 Jan 13 '13 at 18:10
  • You are putting a boolean into a variable and passing it to a function which expects a resource, You can't do that. Suggest reading the documentation on how to use mysql_fetch_assoc and mysql_fetch_array. – Eric Leschinski Jan 13 '13 at 18:10
  • `mysql_query` returned `boolean FALSE`, which means that there was an error. – gd1 Jan 13 '13 at 18:07

1 Answers1

1

As someone else has suggested, your query did not execute successfully. Try using this code to pinpoint the error:

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

Note that the or die() segment can be placed on lots of code in PHP to debug code and figure out exactly where the problems are occurring. The mysql_error() function is also great for the same reason.

SISYN
  • 2,194
  • 4
  • 23
  • 44
  • I'd probably add to this that it's not good practice to die on database errors on a production system - they should be caught and handled more gracefully. But as an immediate way to find out what is going wrong, this will work fine. – halfer Jan 13 '13 at 18:14
  • NVM, i fixed it. noticed a problem i had. Thanks alot! – carl Jan 13 '13 at 18:16
  • @halfer I agree this shouldn't be in a final version of the code, but for debugging purposes I find it much faster unless you already have error handling setup. – SISYN Jan 13 '13 at 18:18
  • @carl No problem, I'm glad you got it working! :) – SISYN Jan 13 '13 at 18:19