 | |
09-09-2006, 04:49 AM
|
· #1 | | Resident Linux Geek Name: Michael Walker Location: East Yorkshire, England Join Date: Aug 2005
Posts: 2,413
NP$: 300.25 ( Donate)
| 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. |
| |
09-09-2006, 06:37 AM
|
· #2 | | NamePros Regular Name: Sukanta Location: Watertown Join Date: Aug 2006
Posts: 437
NP$: 114.45 ( Donate)
| 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();
}
?> |
| |
09-10-2006, 06:01 AM
|
· #3 | | NamePros Member | Thanks a lot Saved me a lot of coding |
| |
10-14-2006, 06:53 AM
|
· #4 | | NamePros Regular Location: Inside your firmware....... Join Date: Oct 2006 | omg, you all know so much about php.. i wish i had the same knoweledge. |
| |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | |