2

Possible Duplicate:
PHP: Warning: sort() expects parameter 1 to be array, resource given

I keep getting this weird error. Yet when I check it, it seems fine.

$stuff=mysql_query('SELECT id, name FROM albums');
$albums_list='';
print_r($stuff);
while ($row=mysql_fetch_array($stuff))
{
    print_r($row);
    $albums_list .= "<option value=" . $row[0]['id'] . ">" . $row[0]['name'] . "</option>\n";
}  

The print_r($stuff); returns

CI_DB_mysql_result Object ( 
    [conn_id] => Resource id #37 
    [result_id] => Resource id #74 
    [result_array] => Array ( ) 
    [result_object] => Array ( ) 
    [current_row] => 0 
    [num_rows] => 1 
    [row_data] =>
)
Community
  • 1
  • 1
SDuke
  • 459
  • 2
  • 4
  • 14

5 Answers5

5

You're getting a CodeIgniter object back. You can use it to call _fetch_assoc() on the instance.

AJ.
  • 26,430
  • 17
  • 82
  • 91
1

That is pretty strange, so the mysql_query is returning an object instead of an result.

try this then:

while ($row=mysql_fetch_array($stuff->result_id))

As thats the resource in the object stack.

RedactedProfile
  • 2,673
  • 6
  • 30
  • 49
1

Wow... strange. Somehow you're getting a CodeIgniter DB object. Are you importing it somewhere? Is your current code using CodeIgniter?

I believe your current resource is at $stuff->result_id, but you might have better luck using the methods here. result_array will probably do what you're trying.

As a side note:

while ($row=mysql_fetch_array($stuff))
{
    print_r($row);
    // don't use row[0]['id']. use row['id']. 
    // You won't get a multi-dimensional array back in any case
    $albums_list .= "<option value=" . $row['id'] . ">" . $row['name'] . "</option>\n";
} 
cwallenpoole
  • 76,131
  • 26
  • 124
  • 163
1

mysql_query() can only return boolean or a resource. Under normal conditions, the error you're getting is not possible.

This means one of three things is likely happening:

  1. Your example code is missing the part where you're actually resigning the value of `$stuff` to a Codeigniter query object.
  2. You are using a modified version of PHP sourcecode. It will not be a fun time for you when it comes time to upgrade PHP.
  3. Using override_function() or runkit_function_redefine(), either you, Codeigniter, or a Codeigniter plugin is reassigning the definition of mysql_query() to return a Codeigniter query object. This creates quite an impracticable dependency.
bob-the-destroyer
  • 3,153
  • 2
  • 22
  • 30
0

Try this:

$stuff=mysql_query('SELECT id, name FROM albums');
$albums_list='';
print_r($stuff);
while ($row=mysql_fetch_assoc($stuff))
{
    print_r($row);
    $albums_list .= "<option value=" . $row['id'] . ">" . $row['name'] . "</option>\n";
}  

OR

You could have the same issue as here: mysqli_fetch_array()/mysqli_fetch_assoc()/mysqli_fetch_row() expects parameter 1 to be resource or mysqli_result, boolean given

Try this:

$stuff=mysql_query("SELECT id, name FROM albums");
Community
  • 1
  • 1
Naftali
  • 142,114
  • 39
  • 237
  • 299
  • Tried both, the first one just changes array to assoc in the error, the second one didn't make a difference. – SDuke Jun 27 '11 at 17:21