Page 1 of 1

"OO"oo the troubles [SOLVED]

Posted: Sun May 27, 2007 3:40 pm
by s.dot
I'm writing a mysql wrapper class, and I'm having troubles understanding the return values of mysql_query();

The manual says this:
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.
So, this is my coding:

Code: Select all

$blog_id	= $db->escape(htmlentities(stripslashes($_GET['blog_id']), ENT_QUOTES));
$result		= $db->query("SELECT * FROM `blogs` WHERE `id` = '85' LIMIT 1");
	
var_dump($result);

echo $db->num_rows($result);
	
if($db->num_rows($result))
{
     //continue...
}
var_dump($result) is returning (bool) true. Where's that coming from?

I get the following errors:

Code: Select all

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\Apache2\htdocs\class\mysql.class.php on line 49
Excerpt from my wrapper class

Code: Select all

/*
* Performs a query on the database and returns the result set
* @param str $sql
*/
function query($sql)
{
	return mysql_query($sql, $this->link_id) or die(mysql_error());
}
	
/*
* Gathers and returns the number of rows from a result set
* @param resource $result
*/
function num_rows($result)
{
	return mysql_num_rows($result);
}
What I can think of is that return cannot return a resource, and instead I should set a $result_resource array in the class properties?

Edit: I should note that $db->query returns (bool) true, regardless if the query returns a result or not.

Posted: Sun May 27, 2007 3:48 pm
by volka

Code: Select all

function query($sql)
{
	$result = mysql_query($sql, $this->link_id) or die(mysql_error());
	echo '<pre>Debug: ', htmlentities($sql), "\n", print_r($result, true), "</pre>\n";
	return $result;
}

There are so many database classes.

Posted: Sun May 27, 2007 3:53 pm
by superdezign
I think you're going about it the wrong way. The way that I do it (not necessarily the RIGHT way, but a way that has yet to fail me) is to have a member variable "$result" in my class, and the Query() function runs the query, and saves the result into $this->result. If $this->result is not null, the query was successful. Otherwise, it returns false.

Then I use the result with other functions like a FetchObject or FetchArray function, but only if the query was successful. I'm not sure how you're getting a "true," but the way you were making your class seemed like you were moving in the wrong direction.

Posted: Sun May 27, 2007 3:54 pm
by s.dot
Ok. Thanks for that. So by assigning the the resource result to a variable, and then returning the variable, the result set will be returned.

Why can't it be directly returned with return mysql_query("...?

(and, writing my own, learning experience)

Posted: Sun May 27, 2007 3:57 pm
by superdezign
Well, it should be able to, but maybe there's something weird in the 'or' statement going through functions, or something else weird somewhere else.

But either way, you want the class itself to handle as much of the database interaction as possible.

Posted: Sun May 27, 2007 4:08 pm
by s.dot
superdezign wrote:I think you're going about it the wrong way. The way that I do it (not necessarily the RIGHT way, but a way that has yet to fail me) is to have a member variable "$result" in my class, and the Query() function runs the query, and saves the result into $this->result. If $this->result is not null, the query was successful. Otherwise, it returns false.

Then I use the result with other functions like a FetchObject or FetchArray function, but only if the query was successful. I'm not sure how you're getting a "true," but the way you were making your class seemed like you were moving in the wrong direction.
What happens if you have multiple queries going on in the same script, and you need to acess $db->result from a previous query? Wouldn't you need an array of results?

I'll post my wrapper class later in the coding critique section.

Posted: Sun May 27, 2007 4:12 pm
by superdezign
Yes, that is a problem, but I have a "$lastResult" and parameters that allow me to access either. I've never needed more than two going at once.

Posted: Sun May 27, 2007 5:33 pm
by s.dot
You were right. The or was causing it to return (boolean) true.

Posted: Sun May 27, 2007 5:35 pm
by superdezign
Booyah! One for me. :-p