Portfolio
Scripts and Websites I've made using PHP and MySQL mainly.
Snippets
Code snippets and mini tutorials
Contact
Contact me if you have any questions or queries
Home / Snippets / connect_to_mysql_database
This section is new and undergoing snippet addition, as of 13/6/11
To connect to a MySQL database using PHP, you will need to first set up the database and create a user account for it. (This can be done via the SQL command line, or whatever control panel you are using, e.g. phpmyadmin, etc...)
To then actually establish a connection, you will need to add the following PHP code (Remembering to change the username, password and database name to the correct values for your database):
Now you should be connected to your database and you can go ahead and run whatever queries on it you like.
This section is new and undergoing snippet addition, as of 13/6/11
Connect to MySQL Database
To connect to a MySQL database using PHP, you will need to first set up the database and create a user account for it. (This can be done via the SQL command line, or whatever control panel you are using, e.g. phpmyadmin, etc...)
To then actually establish a connection, you will need to add the following PHP code (Remembering to change the username, password and database name to the correct values for your database):
$server = 'localhost';
$dbuser = '';
$dbpass = '';
$dbname = '';
$dbc = mysql_connect($server, $dbuser, $dbpass) or die('Cannot Establish Connection To Server - Invalid User/Pass');
mysql_select_db($dbname, $dbc) or die('Cannot Select Database');
Now you should be connected to your database and you can go ahead and run whatever queries on it you like.
Comments
No Comments Have Been Made


