0

I have the following function :

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;
    }       
}   

the error message that I get is : Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\mytest\core\functions\users.php on line 271 Any idea where is the problem with my function?

MMM
  • 7,013
  • 2
  • 23
  • 42
33528
  • 382
  • 6
  • 11
  • 30
  • I suspect the ` in your query. Should it be `'` ? – Luc M Apr 05 '13 at 15:27
  • 2
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](http://j.mp/XqV7Lp). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. – Kermit Apr 05 '13 at 15:29
  • @LucM The ` are valid in MySQL as they are specifying the tables and fields, not strings. – Biotox Apr 05 '13 at 15:30
  • @LucM - you can use the backtick as quotes (kinda) when specifying fields. This lets you have fields called time which would normally clash with a builtin. – slugonamission Apr 05 '13 at 15:30
  • 1
    There are so much ways to handle this kind of issues. Browsing Google for 5 minutes for instance. – Halim Qarroum Apr 05 '13 at 15:33

5 Answers5

1

Your MySQL query failed. You need to consider adding some error checking. Use mysql_error when the result of mysql_query is false.

Halim Qarroum
  • 13,687
  • 4
  • 45
  • 68
datasage
  • 18,795
  • 2
  • 46
  • 53
0

Your connection is most likely out of scope. You need to pass it into your function.

function user_data($user_id, $connect) {
...
}
Kermit
  • 33,206
  • 11
  • 83
  • 119
0

From http://php.net/manual/en/function.mysql-query.php, mysql_query will return FALSE on error. In this case, there's a problem, so it returns FALSE, which gets passed to mysql_fetch_assoc, leading to the error.

As datasage says, use mysql_error to find the problem.

slugonamission
  • 9,467
  • 30
  • 39
0

mysql_query() returns FALSE on fail. Check your $fields variable before using it. And use mysqli instead.

$result = mysql_query("SELECT * FROM $fields") ;
if ($result){
  //continue here, count rows or do something else.
}
sybear
  • 7,789
  • 1
  • 21
  • 38
0

The query " SELECT $fields FROM users WHERE user_id = $user_id " fails for some unknown reason. This leads to mysql_query returning FALSE insterad of a PHP resource.

Call mysql_error() to find out why the query fails.

Oswald
  • 30,441
  • 3
  • 40
  • 68