"OO"oo the troubles [SOLVED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

"OO"oo the troubles [SOLVED]

Post 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.
Last edited by s.dot on Sun May 27, 2007 5:34 pm, edited 1 time in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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)
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

You were right. The or was causing it to return (boolean) true.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Booyah! One for me. :-p
Post Reply