1

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

What is the cause of error in my code and how I can to fix?(i use of codeigniter)

$query_hotel_search = $this->db->query("SELECT * FROM hotel_submits WHERE name LIKE '%$hotel_search%' ORDER BY name asc");
$units = array();
while( $row = mysql_fetch_row( $query_hotel_search ) ) // Line 27
{
   $units = unserialize( $row[0] );
}
echo json_encode(var_dump($units));

Error:

A PHP Error was encountered
Severity: Warning
Message: mysql_fetch_row() expects parameter 1 to be resource, object given
Line Number: 27

Output: array(0) { } null

UPDATE:

Error:

A PHP Error was encountered
Severity: Notice
Message: unserialize() [function.unserialize]: Error at offset 0 of 3 bytes
Line Number: 29

Output: bool(false) null

See my database: https://i.stack.imgur.com/IORSM.jpg

$query_hotel_search = mysql_query("SELECT * FROM hotel_submits WHERE name LIKE '%$hotel_search%' ORDER BY name asc");
            if(mysql_num_rows($query_hotel_search)==0){
                return '0';
            }else{
                $units = array();
                while( $row = mysql_fetch_row( $query_hotel_search ) )
                {
                   $units = unserialize( $row->units[0] ); // Line 29
                }
                echo json_encode(var_dump($units));
            }
Community
  • 1
  • 1
Jennifer Anthony
  • 2,227
  • 10
  • 37
  • 56
  • `$query_hotel_search = $this->db->query("SELECT * FROM hotel_submits WHERE name LIKE '%$hotel_search%' ORDER BY name asc");` – Jennifer Anthony Sep 01 '11 at 22:49
  • Can you put your script? are you using PDO? Custom DB class? or something else? – Book Of Zeus Sep 01 '11 at 22:50
  • Are you sure that function is returning the correct thing? Try doing a `var_dump($query_hotel_search)`. – animuson Sep 01 '11 at 22:50
  • well at first, $this->db->query seems to return a DB Object or result-set Object. mysql_fetch_row requires a mysql_query resource which are different. – Book Of Zeus Sep 01 '11 at 22:53
  • Does your $this->db object has a getRows or similar functionality? – Book Of Zeus Sep 01 '11 at 22:59
  • 1
    It seems she is combining mysql calls with PDO here – JM4 Sep 01 '11 at 23:02
  • please read this: http://codeigniter.com/forums/viewthread/138304/#682255 , What is your conclusion? – Jennifer Anthony Sep 01 '11 at 23:04
  • read the solution above. it use $query = mysql_query($sql); and not $query = $this->db->query($sql); – Book Of Zeus Sep 01 '11 at 23:10
  • that's right. i have new error in code: `$query_hotel_search = mysql_query("SELECT * FROM hotel_submits WHERE name LIKE '%$hotel_search%' ORDER BY name asc"); if($query_hotel_search->num_rows()==0){ return '0'; }else...` error: `Fatal error: Call to a member function num_rows() on a non-object in D:\xampp\htdocs\... on line 16`. !!!? – Jennifer Anthony Sep 01 '11 at 23:16
  • you cannot use $query_hotel_search->num_rows() since it's from the $this->db object, you have to use: mysql_num_rows($query_hotel_search) – Book Of Zeus Sep 01 '11 at 23:18
  • thanks, that's right. sorry i have new error. is update my post please see it again there is new error. – Jennifer Anthony Sep 01 '11 at 23:39
  • 1. change mysql_fetch_row to mysql_fetch_assoc() - and use $row['units'] instead of $row->units[0] – Book Of Zeus Sep 01 '11 at 23:43

2 Answers2

1

http://de2.php.net/manual/en/function.mysql-fetch-row.php

mysql_fetch_row() takes not the query itself but rather the result from the mysql_query command (which is a resource).

Look at the example at the PHP Documentation (linked at the top) and you will get the functionality of mysql_fetch_row.

Maybe the assignment of $query_hotel_search can clarify the situation.

breiti
  • 1,145
  • 9
  • 17
0

You are using a codeigniter object as mysql resource. Instead you should use object iterator provided by codeigniter to loop through the results.

foreach($query_hotel_search->result() as $row)
{
// $row as object
}

foreach($query_hotel_search->result_array() as $row)
{
// $row as array
}

For the second part (UPDATE), mysql_fetch_row() return an enumerated array. so you should use:

$units = unserialize($row[12]);
Vikk
  • 3,353
  • 3
  • 22
  • 24