0

everything was working fine , suddenly this happened :

PHP Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in G:\PleskVhosts\modelcharm.com\httpdocs\core\functions\users.php on line 17

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"));

        return $data;
    }}

Anything wrong in the code ?

Bill the Lizard
  • 386,424
  • 207
  • 554
  • 861
Eng-Amir
  • 80
  • 6
  • yes probably `$func_get_args` is empty. thus fieldlist is empty. thus mysql has syntax error. you can have a better understanding of what is happening by adding `echo mysql_error()` – Volkan Ulukut Feb 19 '14 at 09:52
  • 1
    Many things. You don't check whether the query is successful or not for start. You use outdated mysql_* functions as well. – N.B. Feb 19 '14 at 09:52

2 Answers2

5

It means, that there was an error. This will show you a detailed error message:

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

$data = mysql_fetch_assoc($getData);                 
Christoph Bühler
  • 2,617
  • 1
  • 25
  • 41
  • 2
    Thanks , this helped me .. i changed a field name in database and didn't change it in init.php file ... that was helpful – Eng-Amir Feb 19 '14 at 10:25
0

First of all avoid using mysql_* functions which were deprecated from php 5.5 and start use mysqli_* functions or PDO.

Then avoid using mysql_query() inside mysql_fetch_assoc() because if query fails you will get error. Always use die (mysql_error()) to find whether query has been executed correctly or not

so write coding like this

forphp > 5.2

$con = mysqli_connect("localhost","username","password","DBname");
$query=mysqli_query($con,"SELECT $fields FROM `users` WHERE `user_id` = $user_id") or die (mysqli_error($con));
$data = mysqli_fetch_assoc($query);

for php < 5.3

$query=mysql_query("SELECT $fields FROM `users` WHERE `user_id` = $user_id") or die (mysql_error());
$data = mysql_fetch_assoc($query);
BenMorel
  • 31,815
  • 47
  • 169
  • 296
krishna
  • 3,996
  • 2
  • 27
  • 56