MyBB Community Forums

Full Version: simple_query() vs mysql_query()
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
Is it wrong to use mysql_query() as opposed to your abstraction layer-like functions like simple_query(), insert_query(), etc.

Is the only reason to do this supporting other db storage architectures?
It's advised to use the database class as then the comnection is already made and your wuerkes will be shown in the debug section. It's also useful for cross-database reasons as you stated.
Ah, okay. I thought so, similar to how some other software writes their database information.

Thanks.
Yes, please use the built in class to perform your queries. The class loads an appropriate wrapper for your database. In the case of MySQL the $db->query() command and other like it do eventually use the mysql_query() function of PHP, but with a lot of error checking built in and also a lot of nice features. And since the $db class object and its methods are well defined on the wiki, you should have no problem learning them.
Okay, so I'm looking over the way you set up these classes...

A query that would normally look like this:

$request = mysql_query("
	SELECT value1, value2, value3
	FROM table
	WHERE somefield = somevalue");

$data = array();
while ($row = mysql_fetch_assoc($request))
{
	$data[] = array(
		'my_custom_key' => $row['value1'],
		'my_custom_key' => $row['value2'],
		'my_custom_key' => $row['value3'],
	);
}
mysql_free_result($request);

Following the outline of simple_select(), I could make it so:

$request = $db->simple_select(
	"table", "value1,value2,value3", "somefield = somevalue", array()
);

$data = $db->fetch_array($request);
Yup, that's correct. However, simple select won't work with joins - just something to bare in mind.
(2011-09-15, 06:49 AM)euantor Wrote: [ -> ]Yup, that's correct. However, simple select won't work with joins - just something to bare in mind.

but we do have $db->query() and $db->write_query() (write_query is preferred now) as well Wink
Yeah, I know about them.

May I ask however (never really understood this) what the difference is between query and write_query? I've seen both used, but I've never looked up the difference. Also, why is one preferred over the other? I tend to just use the query method as it's quicker to write.
(2011-09-15, 12:22 PM)euantor Wrote: [ -> ]Yeah, I know about them.

May I ask however (never really understood this) what the difference is between query and write_query? I've seen both used, but I've never looked up the difference. Also, why is one preferred over the other? I tend to just use the query method as it's quicker to write.

I actually asked about this when I was developing plugins because I wasn't sure either Wink

$db->query() works on a single database server only.
$db->write_query() is for executing on a master db and all its slave dbs as well...

Answer to my question when asked:
(2010-08-19, 06:43 PM)Pirata Nervo Wrote: [ -> ]
(2010-08-19, 06:28 PM)Dylan M. Wrote: [ -> ]Pirata, I haven't looked at the differences between query and write_query yet, I suppose I should update my plugins to all use write_query, yes?

write_query executes the query on the slave database ($db->write_link) in case you have a multi-server database setup.
Edit:
So yeah editing your would probably good but it's not a great problem Toungue
Ah! Cheers for that Dylan. Probably time I started using it too then I guess. The wiki should probably be updated to contain that information though - guess I'll do that later.
Pages: 1 2 3