| | |||||
| ||||||||
| 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. |
![]() |
| | LinkBack | Thread Tools |
| | THREAD STARTER #1 (permalink) |
| Senior Member Join Date: Aug 2005 Location: East Yorkshire, England
Posts: 2,689
![]() ![]() ![]() ![]() ![]() ![]() ![]() | 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: |
| |
| | #2 (permalink) |
| NamePros Regular Join Date: Aug 2006 Location: Watertown
Posts: 434
![]() ![]() ![]() | 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();
}
?>
__________________ FreeUSACards | Whois Info | Bulk Whois | 008.us | LinkAlly | Developers Paradise Whois Info | iPhone Board | Make your site Internet Explorer 8 Compatible | Multiple Search | TMV.biz | Whoiz |
| |