Thursday, March 10, 2011

mSQL connection to php






The author would like to present this simple article about how to connect from PHP to MySQL database. This is the result of some feedback that go into Sony AK Knowledge Center and we want to share it in writing. Moreover, there are a lot who are interested in web-based programming with open source solutions, because of free and cheap.
Preparation
In this case the author uses the Win32 version of Apache web server 1.3.33, Windows XP and PHP version 4.3.11. For MySQL, I use version 4.1.11 for Win32. Apache can you download at http://www.apache.org/, can be found at http://www.php.net/ PHP and MySQL can be downloaded from http://www.mysql.com/. The author assumes you've installed all these components and has been running well. Turn all the necessary services, to run the Apache service then type net start apache from the command prompt on Windows. Similarly to the MySQL service, type net start mysql from the command prompt to turn it on. After all ready let's make way connection from PHP to MySQL her.

Sample connection
To connect to MySQL there are two functions that can be used. The first is to use the function mysql_connect. Function syntax is as follows.

resource mysql_connect ([string server [, string username [, string password [, bool new_link [, int client_flags ]]]]])

Examples of its use are as follows.

$ conn = mysql_connect ('localhost', 'root', 'passwordku');
if (! $ conn) {
die ('Connection to MySQL failed:'. mysql_error ());
}
echo 'Connection to MySQL with mysql_connect success!';
mysql_close ($ conn);
?>

We see above there mysql_connect command whose main function is to connect to the MySQL database. As the above example the MySQL server is on localhost with the username root and password is passwordku. There are important notes to remember here. Since version 4.1, MySQL has been using a new system of authentication protocol based on hashing algorithms. This makes the connection using the old client will fail and will generate an error message like the following.

Client does not support authentication protocol requested by server; Consider upgrading MySQL client
During the hold connection then this function will look for a persistent link else already connected to the host, username and password are the same. If the link is found then the identifier of the connection or link will be provided to the client and the client will not be entered into a new connection to the server dataase.
Connection to the database will not be closed when the execution of an SQL script is complete. In the mysql_connect function, the connection will be disconnected automatically shortly after a SQL script is executed. In the mysql_pconnect function, connection to the database will continue to occur and not be disconnected even if you use the mysql_close function.
Basic syntax of the use mysql_pconnect function is as follows.

resource mysql_pconnect ([string server [, string username [, string password [, int client_flags ]]]])

Example of PHP code it from mysql_pconnect function can be seen in the following code.
$ conn = mysql_pconnect ('localhost', 'root', 'ariesa2001');
if (! $ conn) {
die ('Connection to MySQL failed:'. mysql_error ());
}
echo 'Connection to MySQL successfully with mysql_pconnect!!';
?>

In PHP 5, for connection to MySQL already using MySQLi extension (whichabbreviation of MySQL Improved ) . Further information about this can be seen at www.php.net / MySQLi. Writing about these MySQLi hopefully we discuss on another occasion.

Thus the simple inscription on how to connect from PHP to MySQL. May be useful for you all. If any comments or suggestions can be sent to pratiwi.mileniawati @ gmail.com. To read other interesting sites please go to Sony AK Knowledge Center at the address http://www.sony-ak.com/.