GoDaddy Support

Connecting to MySQL Using PHP

Date Submitted: 8/9/14

You can access MySQL databases directly through PHP scripts. This lets you read and write data to your database directly from your website.

To Connect to MySQL Using PHP

  1. Connect to your MySQL server using the mysql_connect statement. For example:

    $con = mysql_connect('HOSTNAME','USERNAME','PASSWORD');

    For help with your mysql_connect information, see Viewing Your Database Details with Shared Hosting Accounts.

  2. Select the database that you want to access using mysql_select_db. For example:

    mysql_select_db('DATABASENAME', $con)
    Where 'DATABASENAME' is the name of your database — this also displays on your database's details page.

After establishing the connection and selecting the database, you can query it using PHP.

To help you create your own connection string, we've included an example below.

Example MySQL Connection String with PHP

<?php //Sample Database Connection Syntax for PHP and MySQL. //Connect To Database $hostname=""; $username="your_dbusername"; $password="your_dbpassword"; $dbname="your_dbusername"; $usertable="your_tablename"; $yourfield = "your_field"; mysql_connect($hostname,$username, $password) or die ("<html><script language='JavaScript'>alert('Unable to connect to database! Please try again later.'),history.go(-1)</script></html>"); mysql_select_db($dbname); # Check If Record Exists $query = "SELECT * FROM $usertable"; $result = mysql_query($query); if($result) { while($row = mysql_fetch_array($result)) { $name = $row["$yourfield"]; echo "Name: ".$name."<br>"; } } ?>

For more information, see the MySQL Functions page at

NEED HELP? Call our award-winning support team 24/7 at (480) 505-8877

United States - English
Traffic Log Image