In the previous MySQL tutorial on Defining and Installing MySQL, you learnt what MySQL is and how it is installed. In this tutorial, you will come to know how you can connect to MySQL database from PHP.
PHP is used a lot with databases, especially, MySQL. It has many advantages for using PHP and MySQL together. For example, My SQL is used with PHP scripts to create dynamic web pages. We can make changes to MySQL from PHP script and fetch data from the database to PHP script vice-versa.
We can connect to MySQL database and make changes in it by executing PHP programs as follows:
<?php $link = mysql_connect('localhost','root','your_password'); if (!$link) { die('Could not connect to MySQL: ' . mysql_error()); } echo 'Connection OK'; mysql_close($link); ?>
Save the program as testmysql.php and run it in your browser by typing http://localhost/testmysql.php.
The output of the code is:
Explanation of above PHP code is as follows:
Mysql_connect is a function which is used to connect to MySQL database. Under the parameters are defined ‘localhost’ as the name of the server, ‘root’ as the user and ‘’your_password‘’ as the password for that ‘root’ user. So, mysql_connect will connect the mysql database by accessing the information given in the parameters. “$link” is just a variable used to store the information of mysql_connect function so that it can be easily used in conditional statements.
If(!$link) specifies the condition that it mysql_connect does not work or the database has not been connected, then it will throw an error according to the statement, die(‘Could not connect to MySQL: ‘ . mysql_error());
If it gets connected, then it should display ‘connection ok’ as per the next statement.
mysql_close($link); at the end of the code closes the connection which was connected in the starting of the program using mysql_connect. In this way, we can connect to the MySQL database by simply writing and executing a PHP code.
Now that you know how to connect to MySQL database using PHP, continue with the next MySQL tutorial on how to create a database in MySQL and also using PHP.
If you have any queries, please leave a comment below.
2 Comments
Hello, sir i would like to ask that what is the scope of Php mysql training, what all topics should be covered and it is kinda bothering me … and has anyone studies from this course http://www.wiziq.com/course/5871-php-mysql-with-basic-javascript-integrated-course of core and advance java online ?? or tell me any other guidance…
would really appreciate help… and Also i would like to thank for all the information you are providing on java concepts.
Hi,
About PHP/MySQL training, i would say it has a good scope and training in this domain will provide you good opportunities. If you are going for professional training, learn basic as well as advance level. Do training where you may get certification that will help in getting entry level jobs. Live project trainings can be very beneficial where you can apply your skill.
So, certification and live projects in PHP/Mysql will be helpful.