PDA

View Full Version : diff betw $vbulletin->db->query_write and $db->query_write


kau
02-27-2009, 06:07 PM
Whats the difference between $vbulletin->db->query_write and $db->query_write and can this effect vbulletin negatively to use the wrong one or cause table locking errors? Thanks

TigerC10
02-27-2009, 06:25 PM
There is no difference. They are the same because of this code:

$db = $vbulletin->db;


It's just that not all of the PHP files have enough queries to warrant creating a $db object. Custom PHP files only have $vbulletin defined in them when you include global, so you'd have to create the $db object yourself (if you really want it).


The only reason for it, is to lighten the lookup load on the PHP engine. It's faster to lookup $db->query_write() than it is to lookup $vbulletin->db->db_query_write(). But it's not really noticibly faster unless you're running thousands upon thousands of queries at once.

Marco van Herwaarden
02-28-2009, 11:18 AM
I doubt it is any faster, it is only shorter to type so saves a bit of work for the developers.

Dismounted
02-28-2009, 12:16 PM
Note that $db is not always in scope - I always use $vbulletin->db to be safe.

TigerC10
02-28-2009, 04:16 PM
I doubt it is any faster, it is only shorter to type so saves a bit of work for the developers.

Under the covers, the PHP engine doesn't have to look through the $vbulletin object which has a lot of other variables in it - consider this psuedo code...

array1 = {23, 1, 453, 87, 6, 2, 7, 9, 10, 135, 37, 134, 64, 1975, 23,753,235,97}

value1 = array1[ indexof(235) ]

In order to call 235 by value (or name) instead of by index, we have to use the "index of" function which searches the array linearly until it finds 235. But if we do that search and assign it to a variable, then we don't have to keep doing that time wasting search.

By calling the db object by name from the $vbulletin object - the PHP engine has to do a linear search in the $vbulletin object. But if you only do that once to assign the db object to $db, you don't have to do that linear search every single time. It is faster to lookup $db than it is to lookup $vbulletin->db.

However, the difference in speed is very small, and it's only noticible if you're doing thousands upon thousands of queries at once.

But as I mentioned before, and as Dismounted pointed out, $db doesn't always exist - and if it doesn't (especially in custom .php files) you would have to define it yourself.