Go Back   vb.org Archive > vBulletin 4 Discussion > vB4 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 12-09-2010, 11:49 PM
LinChang LinChang is offline
 
Join Date: Dec 2010
Posts: 9
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default How to establish database connection for custom Page

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?
Quote:
// ####################### 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());
Reply With Quote
  #2  
Old 12-10-2010, 12:13 AM
Lynne's Avatar
Lynne Lynne is offline
 
Join Date: Sep 2004
Location: California/Idaho
Posts: 41,180
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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
Reply With Quote
  #3  
Old 12-10-2010, 02:43 AM
LinChang LinChang is offline
 
Join Date: Dec 2010
Posts: 9
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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
Reply With Quote
  #4  
Old 12-10-2010, 02:58 AM
calorie calorie is offline
 
Join Date: May 2003
Posts: 2,804
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Tip... if you know you are only looking for one result, you can use query_first instead of query_read
Code:
$info = $db->query_first("SELECT foo FROM table WHERE id = 5");
// $info['foo']
Reply With Quote
  #5  
Old 12-10-2010, 02:53 PM
LinChang LinChang is offline
 
Join Date: Dec 2010
Posts: 9
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by calorie View Post
Tip... if you know you are only looking for one result, you can use query_first instead of query_read
Code:
$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?
Reply With Quote
  #6  
Old 12-10-2010, 03:24 PM
calorie calorie is offline
 
Join Date: May 2003
Posts: 2,804
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

The query_first call would give you $info['foo'] and $info['moo']
and $info['boo'] without doing a fetch_array call. Example...
Code:
$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';
}
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 06:28 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.10485 seconds
  • Memory Usage 2,220KB
  • Queries Executed 13 (?)
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
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (3)bbcode_code
  • (2)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (6)post_thanks_box
  • (6)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (6)post_thanks_postbit_info
  • (6)postbit
  • (6)postbit_onlinestatus
  • (6)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_postinfo_query
  • fetch_postinfo
  • 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
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete