Pre-requisites:
Database host or IP
Database name
Username and password access of db.
First we need to declare some critical data like username and password, so i strongly recommended to store this value into a different file where php will ask tables and data, this for security reason.
In the php convension, usually we make a file called config.inc.php, because the .inc between the file name and real extension, dont show nothing if directly called to your browser.
Let’s make this file usually called config.inc.php
1 2 3 4 5 6 7 | <?php //database parameters $db_host = "localhost"; //usually localhost, but can be an IP $db_user = "myusername"; $db_password = "mypassword"; $db_name = "mydatabasename"; ?> |
Now we must pass to php this values, if you are planning a project, usually is a good idea to make next step to a file called database.php and perform the connection.
1 2 3 4 5 6 7 8 9 10 11 | <?php include("config.inc.php"); //now check our usr/pass credentials $db = mysql_connect($db_host, $db_user, $db_password); if ($db == FALSE) die ("put here an error message"); //now we see if database name can be read/write mysql_select_db($db_name, $db) or die ("ERROR: your parameteres are wrong"); ?> |
With mysql_connect your have ask a connection to database server with your username and password, usually on shared service, your credentials give access to one database, so the server must reply if you have a valid account on it. Be careful, the variables $db_host, $db_user, $db_password must be passed in the order of example above.
With mysq_select_db you ask the permission to read/write on database name, with previous credentials.
Both functions are native PHP functions, and they can perform only a TRUE or FALSE, no database data is exchanged yet.

