PDA

View Full Version : Newbie hacking guide


Martz
17 Jan 2002, 10:42
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.

$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.

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

$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:
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.

Chen
17 Jan 2002, 11:11
Just something random I picked up:
Originally posted by Martz
$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:
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. :D

Martz
17 Jan 2002, 12:01
Ahh, I didn't realise. :) Updated, thats for the info.

Chen
17 Jan 2002, 12:04
Originally posted by FireFly
and the PHP code needs to echo $result[title].
Not $result[result].

Jawelin
05 Feb 2002, 15:27
Originally posted by Martz
[...]$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.

Jawelin
05 Feb 2002, 16:06
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 (http://www.php.net/manual/en/function.mysql-data-seek.php)($results, 0);.
Is it the best way ?
Clean ?

Thanks

Chen
05 Feb 2002, 20:43
Yup Jawelin, that's also the way vBulletin does it (using $DB_site->data_seek()).

Jawelin
05 Feb 2002, 22:38
Thnx.

Palmer ofShinra
20 Feb 2002, 17:52
$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

Alex Taylor
06 Jan 2004, 19:49
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..

MindTrix
06 Jan 2004, 20:38
2) = config.php :p

Kane Da Don
06 Jan 2004, 20:57
thank you for this tutorial this is going to help me out a alot

rrottman
09 Jan 2004, 00:16
thank you for this tutorial this is going to help me out a alot
Small remark:
While

echo $result[title]

works fine, isn't it 100% correct that you should write

echo $result['title']

instead?

NTLDR
09 Jan 2004, 00:24
Indeed the majority of time thats correct, using the first method could produce the wrong result eg:


define('title', 'some text here');

echo $result[title];

// the above would try and echo $result[some text here]

Dean C
09 Jan 2004, 17:30
You only quote your single strings when you are not inbetween any double or single quotes. As:


echo "The forum title is $result['title']";


Will give a nasty error :)

rrottman
09 Jan 2004, 18:41
You only quote your single strings when you are not inbetween any double or single quotes. As:


echo "The forum title is $result['title']";


Will give a nasty error :)
So the correct way would be to assign the value to a non-array var before you echo it with double quotes:


$echostr = $result['title'];
echo "The forum title is $echostr";


I know that $result[title] works, but again, it's syntactically not 100% correct and only works, because PHP is not type safe.

rrottman
11 Jan 2004, 22:07
You only quote your single strings when you are not inbetween any double or single quotes. As:


echo "The forum title is $result['title']";


Will give a nasty error :)
@Mist: That's why PHP usually wants it like this:

echo "The forum title is $result[{'title'}]";