php mysql tutorial

The Easiest Method for Php Mysql Connection



After browsing through the results of my internet search about “how to connect mysql with php” I realized that there are several descriptions and tutorials. Basically they are more or less the same, though for a newbie trying to code something in php for the first time, the following method might easiest:

So, let’s check what needs to be edited to connect your database.

  • localhost: it’s the hostname of your mysql server, generally local host, so you don’t need to edit it
  • username: your mysql username
  • password: your mysql password
  • database: your mysql database

You can test the mysql connection by editing only the connection details above. If you see “php mysql connect – successful” then you have successfully connected to the database. From this point you don’t have to worry about the connection. You can put your code between the two comments, just don’t forget to delete this row: echo “php mysql connect – successful”;



Why you should use mysql_close() after php mysql connect

It’s very important to stop using needless resources. If you have one visitor on your site it won’t make any difference anyway. If you have 10000 visitors a day you should definitely close the mysql connection when it’s not needed. Parallel mysql connections need server resources and probably you want to use them wisely. Only allocate resources when your code uses them.


Alternative method with mysqli

The first one is easier for beginners, though it’s an old method and mysql extension is officially deprecated in PHP 5.5. I found a good article about PDO and MySQLi.

Php mysqli documentation can be found here:



Posted by:

Balazs Hende
I am a blogger and the founder of this site. I have started on October 1, 2013. Have a pleasant stay here!

Related articles

Random article #1


Back to Top