Newbie hacking guide
by
17 Jan 2002
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");
$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");
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");
PHP Code:
echo "The forum title is $result[title]";
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. |