PDA

Click to See Complete Forum and Search --> : Empty set but has a result


nebulom
Apr 14th, 2010, 07:36 PM
I have a weird problem. The query seems to be empty but I got a result resource.

$query = 'SELECT ...';
print_r($this->db . '<br>');
$result = mysql_query($query, $this->db);
print_r($result);
if ($result) {
$price_max_schedules = mysql_result($result, 0, "value");
}
Outputs:
Resource id #72
Resource id #97
I know I'm missing something but I can't seem to find it. Help anyone? Thanks in advance!

kows
Apr 14th, 2010, 10:04 PM
remember that mysql_query() returns a resource on a successful SELECT statement, or false on an error. that doesn't mean something is actually being returned, though -- it just means that the query was successfully executed. then, you're trying to print_r() to resources ($this->db and $result) and because of that you're getting the resource IDs echoed out. you'll need to check mysql_num_rows() to see if a result was actually returned. I'd also suggest using something like mysql_fetch_assoc() if you'd like an easier way of returning values from your result.

a quick example of using mysql_num_rows() and mysql_fetch_assoc():

$sql = "SELECT field1, field2 FROM table";
$result = mysql_query($sql);

//check if a row was returned
if(mysql_num_rows($result) > 0){

$row = mysql_fetch_assoc($result);

print_r($row);

}else{

echo "no results for {$sql}";

}

nebulom
Apr 15th, 2010, 01:09 PM
Thanks man.