Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
  #1  
Old 06-14-2006, 02:11 AM
Red Blaze's Avatar
Red Blaze Red Blaze is offline
 
Join Date: Jan 2003
Location: Texas
Posts: 493
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Constant help needed. Learning how vBulletin works.

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:

PHP 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!
Reply With Quote
  #2  
Old 06-17-2006, 07:21 AM
Alan @ CIT Alan @ CIT is offline
 
Join Date: Nov 2004
Location: South UK
Posts: 625
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Hi,

PHP 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;

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:

PHP Code:
$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:

PHP Code:
// ... 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:

PHP Code:
$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.
Reply With Quote
  #3  
Old 06-17-2006, 08:05 AM
Zachery's Avatar
Zachery Zachery is offline
 
Join Date: Jul 2002
Location: Ontario, Canada
Posts: 11,440
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

shouldn't it be $db-> and not $vbulletin->db-> ?
Reply With Quote
  #4  
Old 06-17-2006, 08:17 AM
Alan @ CIT Alan @ CIT is offline
 
Join Date: Nov 2004
Location: South UK
Posts: 625
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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:

PHP Code:
$vbulletin->db
$vbulletin
->userinfo
$vbulletin
->options 
Rather than use $db, $bbuserinfo and $bboptions, but it's purely personal choice

Thanks,
Alan.
Reply With Quote
  #5  
Old 06-17-2006, 09:04 AM
Zachery's Avatar
Zachery Zachery is offline
 
Join Date: Jul 2002
Location: Ontario, Canada
Posts: 11,440
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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()
Reply With Quote
  #6  
Old 06-17-2006, 09:15 AM
Alan @ CIT Alan @ CIT is offline
 
Join Date: Nov 2004
Location: South UK
Posts: 625
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Hi,

(your right, it was $vboptiions - typo on my part )

You can use $vboptions in the code, it is defined here:

Code:
includes\class_core.php(892): $vboptions      =& $vbulletin->options;
Same goes for $bbuserinfo

Code:
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

PHP Code:
$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:

PHP Code:
$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!

Thanks,
Alan.
Reply With Quote
  #7  
Old 06-18-2006, 05:59 PM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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....72&postcount=5

(With thanks to DAnny for finding the post for me)
Reply With Quote
  #8  
Old 06-18-2006, 06:13 PM
Adrian Schneider's Avatar
Adrian Schneider Adrian Schneider is offline
 
Join Date: Jul 2004
Posts: 2,528
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 11:45 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.03804 seconds
  • Memory Usage 2,255KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (2)bbcode_code
  • (8)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (8)post_thanks_box
  • (8)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (8)post_thanks_postbit_info
  • (8)postbit
  • (8)postbit_onlinestatus
  • (8)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete