NameSilo

2 handy MySQL functions!

SpaceshipSpaceship
SpaceshipSpaceship
SpaceshipSpaceship
Watch

Barrucadu

Established Member
Impact
64
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:
<?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.
 
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
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();
}

?>
 
0
Close
Thanks a lot Saved me a lot of coding
 
0
Close
omg, you all know so much about php.. i wish i had the same knoweledge.
 
0
Close
DomainEasy: white label portfolios are live. Enable yours.

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Filters for activity feeds
Your news feed, Latest activity, and the activity tab on profiles now have filter buttons: All, Posts, Quick replies, and Other. Your...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back