PHP support a lot of database.
MySQL is the most popular uses by PHP. Before you uses MySQL you must make connection to database from php
If i listed the procedure it’s like this :
PHP > Connect Database > Select Database > Manipulate Database (like show, edit, delete) > Close Connection
PHP MySQL Connect
To connect from PHP to MySQL php provide function called mysql_connect followed by some argument provided.
mysql_connect([server],[mysqluser],[mysqlpassword])
Basic argument are most uses :
Server
Tell php to connect with mysql server you can use IP Address or domain
Mysql User
The name of mysql user
MySQL Password
The password for the user
Connection can be done by following example code:
$connection = mysql_connect("192.168.168.20","mashary","passwordmashary");
after You run the code above php will connecting to database, if connection success variable $connection will return boolean value (true) if not it would be false.
When will connection failure? wrong password, wrong username, server down that’s all the possibilities make connection failure. If that happen you can check it out what was going on there by code bellow:
if (!$connection ) { die('Could not connect: ' . mysql_error()); }
Result will give You notice from mysql_error() function if there’s any error and if not result will not display nothing.
Next article will show you how to select database
Tips MySQL Connection
Always use variable like $connection to handle result from connection database because this is very important when you have to work with two or more database.