How can I connect to MySQL Using PHP

PHP has built in support for a number of databases including MySQL, which is by far the most popular database used with PHP.  Using a database with your website is a must for any dynamic or interactive website.  Using a database is quite a simple process as we will explain here.



There are two main steps to connecting to a MySQL database using PHP.

First, connect to your MySQL server using the mysql_connect statement. For example:


You can log in to your hosting account manager to find the hostname, user name, and password for you database.

Next, select the database that you want to access using mysql_select_db. For example:


Where "DATABASENAME" is the name of your database. Again, you can log in to your hosting account manager to find the name of your database.

The name of your database is usually the same as the user name for that database.

From here, you can query your database using your PHP script.


//Sample Database Connection Syntax for PHP and MySQL.

//Connect To Database
$yourfield = "your_field";
mysql_connect($hostname,$username, $password);

# Check If Record Exists

$query = "SELECT * FROM $usertable";

$result = mysql_query($query);

while($row = mysql_fetch_array($result))
$name = $row["$yourfield"];
echo "Name: ".$name."

  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

How do I configure Dreamweaver MX for PHP and MySQL?

Remote Info:Access: FTPFTP Host: domainname.comHost Directory: public_html(this is the folder...

phpMyAdmin error #2002 - The server is not responding (or the local MySQL server's socket is not correctly configured)

If this is your phpMyAdmin error:#2002 - The server is not responding (or the local MySQL...