Wednesday 26 December 2012

Episode 16: How PHP Fits with MySQL



With the onset of PHP5, you need to take a few extra steps to convince PHP and MySQL to play well with each other. Before your MySQL functions will be recognizable by PHP, make sure to enable MySQL in your php.ini file.
You can use MySQL commands within PHP code almost as seamlessly as you do with HTML.

Some of the more commonly used functions are:

mysql_connect ("hostname", "user", "pass"): Connects to the MySQL server.
mysql_select_db("database name"): Equivalent to the MySQL command USE; makes the selected database the active one.
mysql_query("query"): Used to send any type of MySQL command to the server.
mysql_fetch_rows("results variable from query"): Used to return a row of the entire
results of a database query.
mysql_fetch_array("results variable from query"): Used to return several rows of
the entire results of a database query.
mysql_error(): Shows the error message that has been returned directly from the MySQL server.

You will most likely become very familiar with these commands, and many more.
You can also send any MySQL command to the server through PHP and the mysql_query command, as in the preceding example. You do this by sending the straight text through PHP either through a variable or through the mysql_query command directly, like this:

$query = “SELECT * from TABLE”;
$results = mysql_query($query);
You can also do it like this:
$results = mysql_query(“SELECT * from TABLE”);

The results of your query are then put into a temporary array known as $results.

Written by ‘Shojib’.

No comments:

Post a Comment