Previous | Home | Next |
The PHp programmer are connecting the program with MySQL Database , and it's first establish a connection to your web host's MySQL database. This is done with the MySQL connect function.
The MySQL localhost
If you've working in internet , then you using the IP addresses, the IP addresses are used as identifiers for computers and web servers. In this example of a connection script, we assume that the MySQL service is running on the same machine as the script.
When we using the PHP script and MySQL are on the same machine, after that we can use localhost as the address you wish to connect to. The localhost is a shortcut to just have the machine connect to itself. and if your MySQL service is running at a separate location you will need to insert the IP address or URL in place of localhost. Please contact your web host for more details if localhost does not work.
PHP & MySQL Code: <?php mysql_connect ("localhost", "admin", "1admin") or die (mysql_error()); echo "Connected to MySQL<br />"; ?>
Output:
If you load the above PHP script to your webserver and everything works properly, then you should see "Connected to MySQL" displayed when you view the .php page.
The mysql_connect function takes three arguments. Server, username, and password.
In our example above these arguments were:
- Server - localhost
- Username - admin
- Password - 1admin
The "or die(mysql..." code displays an error message in your browser , when you've probably guessed it -- there is an error in processing the connection! Double-check your username, password, or server if you receive this error.
How to Choose the Working Database
After establishing a MySQL connection with the code above, you then need to choose which database you will be using with this connection. This is done with the mysql_select_db function.
PHP & MySQL Code: <?php mysql_connect ("localhost", "admin", "1admin") or die(mysql_error()); echo "Connected to MySQL<br />"; mysql_select_db ("test") or die (mysql_error()); echo "Connected to Database"; ?>
Output:
Previous | Home | Next |