Friday, 8 January 2016

PHP connection with MySQL (mysql_connect)


To make a connection between PHP with MySQL using mysql extension, PHP provides mysql_connect () function.
This function is described as a way to log into the MySQL ServerMysql_connect ().Function requires three arguments, and return value of the function in the form of 'variable' connection to MySQL.
Here is the basic format of writing the function mysql_connect ():
$link = mysql_connect(’mysql_host’, 'mysql_user', 'mysql_password');


  • $ link is the variable that will hold the results of the function mysql_connect (). If successfully connected to MySQL, $ link will contain a value that serves as a 'link' connection with MySQL.Link this connection will be needed throughout the program PHP-MySQL later. In PHP, the variable $ link is known as the handler, ie, variables that handle connections to MySQL. 
  •  mysql host is the first argument of the function mysql_connect (). Value mysql host computer is filled with the address where the MySQL server is running. If you run MySQL using XAMPP on a computer same with a web server running Apache, then this address could be filled with 'localhost' or '127.0.0.1'. However, if the MySQL server is running on another computer, this argument will contain the IP address of that computer. 
  • mysql_user is a MySQL user name where we will be logged. Such as 'root', 'admin', 'andi' and others dependent on MySQL users who have registered on the server.If you are using the default MySQL XAMPP, user 'root' can be used. mysql password filled with the password of the user who made the argument mysql_user.
Variable $ link containing the connection link PHP-MySQL (the result of the function mysql_connect ()), belongs to the group specific data types PHP called resources. Resources variables can not stand alone and is usually used as an argument to another function. You are free to change the name of the variable $ link with other names, such as $ connection, $ link_mysql, etc.
If the connection with PHP failed, mysql_connect () function will return a Boolean value FALSE. FALSE results will be stored in the variable $ link and we can use in IF loop to display the error that occurred.


To find out how to make connections between MySQL with PHP, we just enter the code examples how to use mysql_connect () function.

Here is the PHP code to make a connection with MySQL:
 

No comments:

Post a Comment