PDA

View Full Version : how vbb connects to the db


Dimava
Sun 9th Jun '02, 11:09pm
after looking around a bit, i figured out that:


$DB_site=new DB_Sql_vb;

$DB_site->appname='vBulletin';
$DB_site->appshortname='vBulletin (forum)';
$DB_site->database=$dbname;
$DB_site->server=$servername;
$DB_site->user=$dbusername;
$DB_site->password=$dbpassword;

$DB_site->connect();

$dbpassword="";
$DB_site->password="";


is how vbb connects to the mysql database, can someone explain how this method of connecting, retrieving, and stroing works

thanks

Dimava

nuno
Sun 9th Jun '02, 11:17pm
what's vbb? :confused:

Dimava
Sun 9th Jun '02, 11:39pm
visual bulletin board? aka vbulletin

-Dimava

Dan615
Sun 9th Jun '02, 11:57pm
I don't know about the full version, but in the lite version there's a db_mysql.php include file that defines the MySQL query class (sorry if it's a big long):

<?php
// db class for mysql
// this class is used in all scripts
// do NOT fiddle unless you know what you are doing

class DB_Sql_vb {
var $database = "";

var $link_id = 0;
var $query_id = 0;
var $record = array();

var $errdesc = "";
var $errno = 0;
var $reporterror = 1;

var $server = "localhost";
var $user = "root";
var $password = "";

var $appname = "vBulletin Lite";
var $appshortname = "vBulletin Lite (cp)";

function connect() {
// connect to db server

if ( 0 == $this->link_id ) {
if ($this->password=="") {
$this->link_id=mysql_pconnect($this->server,$this->user);
} else {
$this->link_id=mysql_pconnect($this->server,$this->user,$this->password);
}
if (!$this->link_id) {
$this->halt("Link-ID == false, connect failed");
}
if ($this->database!="") {
if(!mysql_select_db($this->database, $this->link_id)) {
$this->halt("cannot use database ".$this->database);
}
}
}
}

function geterrdesc() {
$this->error=mysql_error();
return $this->error;
}

function geterrno() {
$this->errno=mysql_errno();
return $this->errno;
}

function select_db($database="") {
// select database
if ($database!="") {
$this->database=$database;
}

if(!mysql_select_db($this->database, $this->link_id)) {
$this->halt("cannot use database ".$this->database);
}

}

function query($query_string) {
// do query

$this->query_id = mysql_query($query_string,$this->link_id);
if (!$this->query_id) {
$this->halt("Invalid SQL: ".$query_string);
}

return $this->query_id;
}

function fetch_array($query_id=-1) {
// retrieve row
if ($query_id!=-1) {
$this->query_id=$query_id;
}
$this->record = mysql_fetch_array($this->query_id);

return $this->record;
}

function free_result($query_id=-1) {
// retrieve row
if ($query_id!=-1) {
$this->query_id=$query_id;
}
return @mysql_free_result($this->query_id);
}

function query_first($query_string) {
// does a query and returns first row
$this->query($query_string);
$returnarray=$this->fetch_array($this->query_id);
$this->free_result($this->$query_id);
return $returnarray;
}

function data_seek($pos,$query_id=-1) {
// goes to row $pos
if ($query_id!=-1) {
$this->query_id=$query_id;
}
$status = mysql_data_seek($this->query_id, $pos);
return $status;
}

function num_rows($query_id=-1) {
// returns number of rows in query
if ($query_id!=-1) {
$this->query_id=$query_id;
}
return mysql_num_rows($this->query_id);
}

function insert_id() {
// returns last auto_increment field number assigned

return mysql_insert_id($this->link_id);

}

function halt($msg) {
$this->errdesc=mysql_error();
$this->errno=mysql_errno();
// prints warning message when there is an error
global $technicalemail;
$message="Database error in $this->appname: $msg\n";
$message.="mysql error: $this->errdesc\n";
$message.="mysql error number: $this->errno\n";
$message.="Date: ".date("l dS of F Y h:i:s A")."\n";
$message.="Script: ".getenv("REQUEST_URI")."\n";
$message.="Referer: ".getenv("HTTP_REFERER")."\n";

mail ($technicalemail,"$this->appshortname Database error!",$message);

if ($this->reporterror==1) {
echo "\n<!-- $message -->\n";

echo "</td></tr></table>\n<p>There seems to have been a slight problem with the database.\n";
echo "Please try again by pressing the refresh button in your browser.</p>";
echo "An E-Mail has been dispatched to our <a href=\"mailto:$technicalemail\">Technical Staff</a>, who you can also contact if the problem persists.</p>";
echo "<p>We apologise for any inconvenience.</p>";
die("");
}
}
}
?>

It's actually pretty simple...sorry if this is bad that I just posted the vB Lite Code :cool:


Oh, and it's initialized in the global.php file, with DB_site = new DB_Sql_vb;

andrewpfeifer
Mon 10th Jun '02, 12:03am
Ahh! Thought you were crazy for a second and posted the whole file out of 2.2.6. However, they appear to be quite similar...

Dimava
Mon 10th Jun '02, 12:19am
ooh ok, i never looked at it as a whole thing, was just looking at it part by part

megahard
Mon 10th Jun '02, 9:57am
very basic OOP but save a lot of time, i prefer to get each variable ($this->numrows, $this->fetcharray) etc in one function, and then just use them as variables throughout the class or in my script.


$DB_site=new DB_Sql_vb;

$DB_site->server=$servername;
$DB_site->user=$dbusername;
$DB_site->password=$dbpassword;

takes them three variables, and sticks them in here:

$this->link_id=mysql_pconnect($this->server,$this->user,$this->password);


of course that's very basic, it does save a lot of time.