PDA

View Full Version : How to establish database connection for custom Page


LinChang
12-09-2010, 11:49 PM
Ok so I am creating a custom page, everything went well but am stuck on one little issue, that is how do i establish connection to database in vBulletin 4.x, I know how to establish database connection but I just want to know if their is any built in function in vbulletin 4.x that does the connection to establishment automatically. Below is my demo code, scroll down the the very bottom in red is where I showed a example where I said mysql_query($sql) but before I can do that I first need to connect to database. Anyone know the answer?

// ####################### SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE);
// #################### DEFINE IMPORTANT CONSTANTS #######################
define('THIS_SCRIPT', 'test');
define('CSRF_PROTECTION', true);
// change this depending on your filename
// ################### PRE-CACHE TEMPLATES AND DATA ######################
// get special phrase groups
$phrasegroups = array();
// get special data templates from the datastore
$specialtemplates = array();
// pre-cache templates used by all actions
$globaltemplates = array('films',
);
// pre-cache templates used by specific actions
$actiontemplates = array();
// ######################### REQUIRE BACK-END ############################
// if your page is outside of your normal vb forums directory, you should change directories by uncommenting the next line
// chdir ('/path/to/your/forums');
require_once('./global.php');
// ################################################## #####################
// ######################## START MAIN SCRIPT ############################
// ################################################## #####################
$navbits = construct_navbits(array('' => 'Asia Films'));
$navbar = render_navbar_template($navbits);
// ###### YOUR CUSTOM CODE GOES HERE #####


How does vBulletin 4.x establish the mysql connection? so that my mysql_query below will be able to connect to the database and grab the stuff

$variable = mysql_query($sql)or die();
//Below is the code that I am going to use to manipulate the returned mysql query.


// ###### NOW YOUR TEMPLATE IS BEING RENDERED ######
$templater = vB_Template::create('films');
$templater->register_page_templates();
$templater->register('headcustom', $headcustom);
$templater->register('navbar', $navbar);
$templater->register('pagetitle', $pagetitle);
$templater->register('page', $page);
print_output($templater->render());

Lynne
12-10-2010, 12:13 AM
Since you included global.php, then you can just write the queries like they are written in the rest of the vb files - SQL Query Syntax (http://www.vbulletin.com/docs/html/main/codestandards_sql_query)

LinChang
12-10-2010, 02:43 AM
ahhhh THANKS.... IT WORKS hahahaha.....

Oh and also for those of you who wants to know how to grab and fetch stuff from database then this is the code
Before you can use the code below you first need to import global.php by doing

require_once('./global.php');

$results = $db->query_read(
"YOUR SQL QUERY GOES HERE"
);
while($rows = $db->fetch_array($results))
{
//This will walk through each row that is returned from $result and stored them in $rows on each walk through, if your query only returned 1 row results maximum then no need to put $rows = $db->detch_array($results) inside while loop
}

Hope the above code will help out some of you guys

calorie
12-10-2010, 02:58 AM
Tip... if you know you are only looking for one result, you can use query_first instead of query_read :)

$info = $db->query_first("SELECT foo FROM table WHERE id = 5");
// $info['foo']

LinChang
12-10-2010, 02:53 PM
Tip... if you know you are only looking for one result, you can use query_first instead of query_read :)

$info = $db->query_first("SELECT foo FROM table WHERE id = 5");
// $info['foo']


Then do you still need to fetch the array? or by just doing $info['foo'] your able to get that result?

Also what about lets say query_read

for example if I am grabbing a row with which contains more than one column, so say

$info = $db->query_first("SELECT foo,moo,boo FROM table WHERE id = 5");

which selects foo,moo,boo where id=5

so can you do $info['foo'] $info['moo'] $info['boo'] to get those three column result or should you fetch_array first?

calorie
12-10-2010, 03:24 PM
The query_first call would give you $info['foo'] and $info['moo']
and $info['boo'] without doing a fetch_array call. Example...

$info = $db->query_first("
SELECT foo, moo, boo
FROM table
WHERE id = 5
");

if ($info)
{
echo $info['foo'] . ' ' .
$info['moo'] . ' ' .
$info['boo'];
}
else
{
echo 'bust';
}