Go Back   vb.org Archive > vBulletin Article Depository > Read An Article > vBulletin 3 Articles
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools
Martz's Avatar
Martz
Join Date: Oct 2001
Posts: 156

 

UK
Show Printable Version Email this Page Subscription
Martz Martz is offline 01-16-2002, 10:00 PM

I thought I would post my experience of learning php and the way vB works under the hood. Please contribute if you have anything to add or if I need correcting!

Why?
My reason for posting this is because I think learning to modify your vBulletin source is quite confusing if you keep refering to the php/mySQL site to try and understand their code. vB uses its own functions which actually save you time and help you quickly develop your idea and keep it consistant throughout the forum. These snippets of code are precious in my opinion, and I thought I would share what I have learnt so far!

Firstly you should obviously read the "Do's and Don'ts" thread in this forum to make sure you don't screw everything up. Even better, install a second vB (which is private and non operational) and that also uses a second database. If you do break/corrupt anything its not going to matter.

On with the show..

Getting information (queries)
Instead of using the standard basic mySQL function to connect to the forum database and get/put information as documented on the php site - vBulletin has its own function which does this in a cleaner way.

PHP Code:
$results=$DB_site->query("SELECT * FROM user"); 
What it does in english:
$DB_site->query is the piece of magic here. All of the stuff that you may have read on on the php.net site concerning connecting to databases - you can forget for the moment. $DB_site->query is great for quickly and easily grabbing information, however complex the query without worring about the details of the connection. The variable $results becomes equal to everything (*) in the user table and stored as an array ($results) with many sets.

Next we need to cycle though all of these records and either extract or use the information we have. This can be done with a while statement and also the fetch_array function built into vB for us to take advantage of! fetch_array is a condition which can be used in the while statement to "Keep looping until we're all out of records".

Tip!: When picking variable names, keep them logical. For queries which return an array, use a plural (such as $results not $result). For queries which make use of query_first or when looping though an array, use a singular variable name to indicate you are being specific to 1 record and not all of the information.

PHP Code:
$results=$DB_site->query("SELECT * FROM user WHERE userselect=1");
// the above is our previous query which we'll use for this example

        
while ($result=$DB_site->fetch_array($results)) {
// start a loop which will: 
// assign the contents of the current record in the loop to the variable $result.

echo "Username: $result[username]";
echo 
"Title: $result[title]";

        }
// end of while loop 

Sometimes though, we know we only want to get 1 record which has a unique feature or condition. Example: "select the forum title from the forum table where the forumid = 6". This can be done using a query_first connection as below:

PHP Code:
$result=$DB_site->query_first("SELECT title FROM forum WHERE forumid=6"); 
As there will only be 1 forum at the most in the database with a forumdid of 6 - we can use query_first. $result is returned as a normal array, unlike the standard query function which returns a set of arrays. This can be used just as:
PHP Code:
echo "The forum title is $result[title]"
Since we only have 1 set (an array) of information we do not have to use a while statement to loop through the records.

Thats all folks!

For the moment anyway. If anyone else with more hacking experience has ways in which we can save time and keep things simple - please reply. I'll try and post how to build tables in the admin cp and how variables are passwed between forms.

HTH someone! Its taken me long enough to work out. Comments, errors and revisions welcomed.
Reply With Quote
  #2  
Old 01-17-2002, 09:11 AM
Admin's Avatar
Admin Admin is offline
Coder
 
Join Date: Oct 2023
Location: Server
Posts: 1
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Just something random I picked up:
Quote:
Originally posted by Martz
PHP Code:
$result=$DB_site->query_first("SELECT title FROM forum WHERE forumid=6"); 
As there will only be 1 forum at the most in the database with a forumdid of 6 - we can use query_first. $result is returned as a normal variable, unlike the standard query function which returns an array. This can be used just as:
PHP Code:
echo "The forum title is $result"
Should say:
$result is returned as a normal array
and the PHP code needs to echo $result[title].

Also when you use query(), you are not getting an array, but a result set (of MySQL). When you use query_first() you get an array.

Great thread.
Reply With Quote
  #3  
Old 01-17-2002, 10:01 AM
Martz's Avatar
Martz Martz is offline
 
Join Date: Oct 2001
Location: UK
Posts: 156
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Ahh, I didn't realise. Updated, thats for the info.
Reply With Quote
  #4  
Old 01-17-2002, 10:04 AM
Admin's Avatar
Admin Admin is offline
Coder
 
Join Date: Oct 2023
Location: Server
Posts: 1
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally posted by FireFly
and the PHP code needs to echo $result[title].
Not $result[result].
Reply With Quote
  #5  
Old 02-05-2002, 01:27 PM
Jawelin Jawelin is offline
 
Join Date: Nov 2001
Posts: 557
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally posted by Martz
[...]
PHP Code:
$results=$DB_site->query("SELECT * FROM user WHERE userselect=1");
        while (
$result=$DB_site->fetch_array($results)) {
// start a loop which will: 
// assign the contents of the current record in the loop to the variable $result.
        
}
// end of while loop 
Sorry for bumping, but didn't found a better place...
Well, just for hacking purposes, I would need to 'surf' the above $results set.
How could I - for example - reset the fetch pointer without resending the query ?
Is that set just like a stack or each fetch_xxx() I'll loose at all the previous record ?
Thanks.
Reply With Quote
  #6  
Old 02-05-2002, 02:06 PM
Jawelin Jawelin is offline
 
Join Date: Nov 2001
Posts: 557
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally posted by Jawelin

Sorry for bumping, but didn't found a better place...
Well, just for hacking purposes, I would need to 'surf' the above $results set.
How could I - for example - reset the fetch pointer without resending the query ?
Is that set just like a stack or each fetch_xxx() I'll loose at all the previous record ?
Thanks.
Found mysql_data_seek($results, 0);.
Is it the best way ?
Clean ?

Thanks
Reply With Quote
  #7  
Old 02-05-2002, 06:43 PM
Admin's Avatar
Admin Admin is offline
Coder
 
Join Date: Oct 2023
Location: Server
Posts: 1
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Yup Jawelin, that's also the way vBulletin does it (using $DB_site->data_seek()).
Reply With Quote
  #8  
Old 02-05-2002, 08:38 PM
Jawelin Jawelin is offline
 
Join Date: Nov 2001
Posts: 557
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thnx.
Reply With Quote
  #9  
Old 02-20-2002, 03:52 PM
Palmer ofShinra's Avatar
Palmer ofShinra Palmer ofShinra is offline
 
Join Date: Oct 2001
Location: Vancouver, BC, Canada
Posts: 171
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

$DB_site is my friend...

Saves SOOOOOOO much coding.

A suggestion... get under the $DB_site hood...

Go to your admin folder and open db_mysql.php

That contains the complete code for the $DB_site object, including all it's methods, like insert_id and stuff...

IMPORTANT TO NOTE:

If you want to use $DB_site (and other standard vB variables, like $bbuserinfo) you need to start your script with a
require("global.php");

And to use $DB_site in a FUNCTION, you have to declare it as a global

global $DB_site;

Otherwise you'll get slapped with parse errors talking about member functions of a non-object
Reply With Quote
  #10  
Old 01-06-2004, 05:49 PM
Alex Taylor Alex Taylor is offline
 
Join Date: Nov 2003
Posts: 8
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Major bump :P

Just wondering..

1) Can these functions be used to connect to another database?
2) Where are the connection details for the database hidden? Eg. username/password, database name..
Reply With Quote
Reply


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 01:29 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04927 seconds
  • Memory Usage 2,314KB
  • Queries Executed 23 (?)
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
  • (7)bbcode_php
  • (4)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)modsystem_article
  • (1)navbar
  • (4)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (9)postbit
  • (10)postbit_onlinestatus
  • (10)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
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete