NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming > CODE
Reload this Page 2 handy MySQL functions!

CODE This forum is for posting code snippets and example scripts that aren't quite tutorials, but could be useful for others. You may post code snippets and/or completed scripts that you've written and want to share here.

Advanced Search
7 members in live chat ~  


Closed Thread
 
LinkBack Thread Tools
Old 09-09-2006, 04:49 AM THREAD STARTER               #1 (permalink)
Senior Member
 
Barrucadu's Avatar
Join Date: Aug 2005
Location: East Yorkshire, England
Posts: 2,689
Barrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu 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'] = '';
????: NamePros.com http://www.namepros.com/code/236229-2-handy-mysql-functions.html

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 />';
????: NamePros.com http://www.namepros.com/showthread.php?t=236229
    
$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.
Barrucadu is offline  
Old 09-09-2006, 06:37 AM   #2 (permalink)
NamePros Regular
Join Date: Aug 2006
Location: Watertown
Posts: 434
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  
Old 09-10-2006, 06:01 AM   #3 (permalink)
NamePros Member
Join Date: Oct 2005
Posts: 40
gauravmm is an unknown quantity at this point
 



Thanks a lot Saved me a lot of coding
gauravmm is offline  
Old 10-14-2006, 06:53 AM   #4 (permalink)
NamePros Regular
Join Date: Oct 2006
Location: Inside your firmware.......
Posts: 217
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  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


 
All times are GMT -7. The time now is 02:57 PM.

Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger