-1

I have been working on the following function which creates an array and adds two integers to it based off of the last two rows in a database.

    function featuredUsers(){
    $users = array();
    $query = mysql_query("SELECT * FROM `featured_users` ORDER BY unique DESC LIMIT 2");
    while ($row = mysql_fetch_assoc($query)) {
        array_push($users, $row["uid"]);  
    }

    return $users;
  }

"Unique" uniquely identifies each row, and "uid" contains the user ids that we are trying to add to the array. However, the array remains empty, and the console returns no errors besides "mysql_fetch_assoc() expects parameter 1 to be resource, boolean...". No other functions in our code use a resource as the first parameter.

Is there a simple error I'm overlooking?

Austin Berke
  • 85
  • 1
  • 7
  • mysql_query() returns TRUE on success or FALSE on error – Nouphal.M Feb 11 '14 at 03:57
  • @Nouphal.M Wrong, it returns resource on success. Otherwise, how can u loop thru true!! Austine Berke, Pls don't use mysql_query since it's deprecated in new php. And pls read this http://my1.php.net/mysql_query . The function is returning FALSE, did u try to debug $query? – Mohammed Joraid Feb 11 '14 at 04:09
  • Yes thats right for the case of select statement – Nouphal.M Feb 11 '14 at 04:15

3 Answers3

0

You're probably not selecting a/the right database. Answer can probably found here: https://stackoverflow.com/questions/9383044/mysql-fetch-assoc-expects-parameter-1-to-be-resource-boolean-given-error

Ruben
  • 1,366
  • 3
  • 16
  • 25
0

Maybe you missing to put a reference column after ORDER BY. My understand (you can correct if it wrong) is after ORDER BY, you must be following with a referrence column. Since i'm not familiar with UNIQUE syntax, i really appreciate with more information about this syntax.

Good luck!

irvana
  • 233
  • 1
  • 5
  • 17
0

I think you are doing in the wrong way. Try this.

function featuredUsers(){
    $users = array();
    $query = mysql_query("SELECT distinct(uid) FROM `featured_users` ORDER BY uid DESC LIMIT 2");
    while ($row = mysql_fetch_assoc($query)) {
        array_push($users, $row["uid"]);  
    }

    return $users;
  }
Amit Garg
  • 3,757
  • 1
  • 29
  • 34