[advanced search]
19 members in the live chat room. Join Chat!
Register Rules & FAQ NP$ Store Active Threads Mark Forums Read
Go Back   NamePros.Com > Design and Development > Programming > CODE
User Name
Password

Old 09-09-2006, 04:49 AM   · #1
Mikor
Resident Linux Geek
 
Mikor's Avatar
 
Name: Michael Walker
Location: East Yorkshire, England
Trader Rating: (7)
Join Date: Aug 2005
Posts: 2,413
NP$: 300.25 (Donate)
Mikor is a splendid one to beholdMikor is a splendid one to beholdMikor is a splendid one to beholdMikor is a splendid one to beholdMikor is a splendid one to beholdMikor is a splendid one to behold
2 handy MySQL functions!

Functions:
set_database_vars($mysql_server, $mysql_username, $mysql_password, $database_name) - Stores the MySQL conenction information. Returns an error message if it fails.

sql($query) - Creates a mysql connection, queries the database, and closes the conenction. Returns the results if sucecssful, if it fails, it returns an error message.

Code:
PHP Code:
<?php

$mod_db
['Server'] = '';
$mod_db['Username'] = '';
$mod_db['Password'] = '';
$mod_db['Name'] = '';

function
set_database_vars($server, $username, $password, $name){
    
$mod_db['Server'] = $server;
    
$mod_db['Username'] = $username;
    
$mod_db['Password'] = $password;
    
$mod_db['Name'] = $name;
    
    return
true;
}

function
sql($query){
    
mysql_connect($mod_db['Server'], $mod_db['Username'], $mod_db['Password'], $mod_db['Name'])or return 'Error 1 - Please check your database variables are correct.<br /><br />';
    
$result = mysql_query($query)or return 'Error 2 - Please check your SQL query is correct for your version of MySQL.<br /><br />';
    return
$result;
    
mysql_close();
}

?>


Handy if you need to do a lot of queries - just include that code in a seperate file and you can query in 1 line of code.


Please register or log-in into NamePros to hide ads
__________________
Me | Blog | Last.fm | F@h

archlinux User
Mikor is offline   Reply With Quote
Old 09-09-2006, 06:37 AM   · #2
sukantab
NamePros Regular
 
Name: Sukanta
Location: Watertown
Trader Rating: (10)
Join Date: Aug 2006
Posts: 437
NP$: 114.45 (Donate)
sukantab is a jewel in the roughsukantab is a jewel in the roughsukantab is a jewel in the rough
If I need to use queries extensively, I would create a global connection variable dbconn. Because connecting to a db is always memory consuming.
Also, don't close conn. and in your code the close will not be executed because you are doing a return before it.

Please send me your comments....

Code:
<?php $mod_db['Server'] = ''; $mod_db['Username'] = ''; $mod_db['Password'] = ''; $mod_db['Name'] = ''; $dbconn = null; function set_database_vars($server, $username, $password, $name){ $mod_db['Server'] = $server; $mod_db['Username'] = $username; $mod_db['Password'] = $password; $mod_db['Name'] = $name; return true; } function sql($query){ if($dbconn=null) { $dbconn = mysql_connect($mod_db['Server'], $mod_db['Username'], $mod_db['Password'], $mod_db['Name'])or return 'Error 1 - Please check your database variables are correct.<br /><br />'; } $result = mysql_query($query)or return 'Error 2 - Please check your SQL query is correct for your version of MySQL.<br /><br />'; return $result; // mysql_close(); } ?>
sukantab is offline   Reply With Quote
Old 09-10-2006, 06:01 AM   · #3
gauravmm
NamePros Member
 
Trader Rating: (0)
Join Date: Oct 2005
Posts: 40
NP$: 64.00 (Donate)
gauravmm is an unknown quantity at this point
Thanks a lot Saved me a lot of coding
gauravmm is offline   Reply With Quote
Old 10-14-2006, 06:53 AM   · #4
boebi
NamePros Regular
 
Location: Inside your firmware.......
Trader Rating: (7)
Join Date: Oct 2006
Posts: 216
NP$: 0.00 (Donate)
boebi will become famous soon enoughboebi will become famous soon enough
omg, you all know so much about php.. i wish i had the same knoweledge.
boebi is offline   Reply With Quote
Closed Thread

NamePros is a revenue sharing forum.

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


Site Sponsors
http://www.mobisitetrader.com/ http://www.mobisitetrader.com/ Get Me Visits
Advertise your business at NamePros
All times are GMT -7. The time now is 11:40 PM.


Powered by: vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 2.4.0