View Full Version : Constant help needed. Learning how vBulletin works.
Red Blaze
06-14-2006, 02:11 AM
I looked at some tutorials, but I still don't understand it clearly. It looks like a regular query, but... I feel uncertain, still.
Example: I plan to create a chat hack for my forums (I plan to share), but I don't know how vBulletin works. I know very small details, but not enough. I still need to understand how to run queries to gather information from a user.
When a user enters the page, php checks if the user is banned from the chat. In user, there's a table called chat_banned. With a value that would rather be a 1 or 0. (1 = True, 0 = False). And it also checks a group. In the same user table, there's a table called chat_group. Values would be one of the following: Admin, Mod, Member.
I need to tell php to get that info from MySQL. I know how to do it in a regular website, but this is vBulletin I'm getting into, and I'm not sure how the coding works.
I would really appreciate the help, and will be given credit in my planned hack.
I found some ways around, but now I'm in another confusing spot. This is my code:
$status = $vbulletin->db->query_read("
SELECT *
FROM " . TABLE_PREFIX . "chat_info
WHERE chat_id = 1
");
if($status['status'] != 'Active'){
eval('print_output("' . fetch_template('cs_closed') . '");');
exit;
}
..And it's always saying it's closed. The status value is Active, in the database. But it's not reading that. What ever it's reading, or not reading, it doesn't say Active, and I think that's why it's always giving me that. What am I missing? Thank you for your advice!
Alan @ CIT
06-17-2006, 07:21 AM
Hi,
$status = $vbulletin->db->query_read("
SELECT *
FROM " . TABLE_PREFIX . "chat_info
WHERE chat_id = 1
");
if($status['status'] != 'Active'){
eval('print_output("' . fetch_template('cs_closed') . '");');
exit;
}
The problem there is that you are running the query (query_read()) but not fetching an array of the results :)
When running a query, there are 2 parts.
1) query_read() - This runs the query and returns the result
2) fetch_array() - This converts the result into something PHP can understand
So, for your query, you need to change the code to:
$result = $vbulletin->db->query_read("
SELECT *
FROM " . TABLE_PREFIX . "chat_info
WHERE chat_id = 1
");
$status = $vbulletin->db->fetch_array($result);
if($status['status'] != 'Active'){
eval('print_output("' . fetch_template('cs_closed') . '");');
exit;
}
The fetch_array function will return 1 row at a time, so if your query returns more than 1 row, then you need to use it in a while() loop. Eg:
// ... run query here
while ($status = $vbulletin->db->fetch_array($result))
{
// Do something to the returned table row
}
vBulletin also includes a handy function called query_first(). This runs the query, and returns the first row from the result without having to call fetch_array() yourself.
For a query like yours, where there will only be 1 row returns, query_first() is ideal.
So, your code using query_first() would look like:
$status = $vbulletin->db->query_first("
SELECT *
FROM " . TABLE_PREFIX . "chat_info
WHERE chat_id = 1
");
if($status['status'] != 'Active'){
eval('print_output("' . fetch_template('cs_closed') . '");');
exit;
}
Hope all that makes sense, and good luck with your script :)
Thanks,
Alan.
Zachery
06-17-2006, 08:05 AM
shouldn't it be $db-> and not $vbulletin->db-> ?
Alan @ CIT
06-17-2006, 08:17 AM
Both work fine, they both point to the same place. I prefer to use $vbulletin->db however because it gives a consistant feel to it all.
eg:
$vbulletin->db
$vbulletin->userinfo
$vbulletin->options
Rather than use $db, $bbuserinfo and $bboptions, but it's purely personal choice :)
Thanks,
Alan.
Zachery
06-17-2006, 09:04 AM
Well $bbuserinfo and $bboptions (isn't it vBoptions?) are not avaible in the php code, you need to use the class refrences (is that correct? or is it class varibles?, whatever) in order to get that data unless I'm guessing you copied the array to the $bbuserinfo varible.
$db-> works for 3.5/3.6. And I would think its easier while browsing and looking for queries its easier to pick out $db->query() compared to $vbulletin->db->query()
Alan @ CIT
06-17-2006, 09:15 AM
Hi,
(your right, it was $vboptiions - typo on my part :))
You can use $vboptions in the code, it is defined here:
includes\class_core.php(892): $vboptions =& $vbulletin->options;
Same goes for $bbuserinfo
includes\class_core.php(894): $bbuserinfo =& $vbulletin->userinfo;
But as vBulletin is moving over to object-orientated code, you should access these vars using the registry ($vbulletin->). In my opinion, that goes for $db as well, you should access that via the registry (ie, $vbulletin->db), but again - it's really personal choice, just as you can still use $bbuserinfo and $vboptions in your PHP code if you want :)
Also, with regards to looking for queries, that's not a problem for me as all my queries are in the format
$var_q = "..query..", so a quick search for "_q =" would find them all :)
Incase anyones wondering I use _r for the sql resultset, and _a for the array :) eg:
$fetch_posts_q = "...sql to fetch posts...";
$fetch_posts_r = $vbulletin->db->query_read($fetch_posts_q);
while ($fetch_posts_a = $vbulletin->db->fetch_array(...))
// I find this easier to read/understand when looking back at old code than
$query = ...
$result = ...
$array = ...
Descriptive variable names are the key! :D
Thanks,
Alan.
Marco van Herwaarden
06-18-2006, 05:59 PM
To continue on the $db versus $vbulletin->db:
The object that is used is $vbulletin->db.
To save on the keyboards of the developers ;) $db is defined as a reference to the addresspace of $vbuylletin->db in the top-level scope.
This means that you can exchange $db and $vbulletin->db if you want, they both point to the same object.
If you however go into a function for example, then it will probably not have $db in it's scope. You will find also stock vB files where not $db is used but $vbulletin->db because $db is not in the scope.
To avoid confusion and scope problems, i personally also always code the full qualification to the object, ie. $vbulletin->db. Like that is is maybe a bit more typing, but i can always use that, and it is more "correct" on an object oriented environment.
You might find some guidelines over here: https://vborg.vbsupport.ru/showpost.php?p=663572&postcount=5
(With thanks to DAnny for finding the post for me)
Adrian Schneider
06-18-2006, 06:13 PM
I always use $db in frontend files, but of course use the correct object when using classes. I do it for readability, and because it is faster to type.
$bbuserinfo and $vboptions are different because they aren't available (as said) in the PHP files, though running legacy_enable() will allow you to use them. Templates automatically convert them to their $vbulletin->X counterparts ($GLOBALS['vbulletin']->X I think).
They say not to use $db in functions, because you will either have to global it as well ($vbulletin and $db) or create a reference to it.
Anyway, not a big deal either way... personal preference by the looks of it.
Edit: By the way, you don't need to exit; after using print_output(), because it will do it for you.
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.