MySQL Connection String with PHP
Print this Article
Comment on this Article
Last Updated:
September 8, 2008 9:44 AM
To connect to your MySQL database using PHP you will need a connection string. This is a PHP MySQL Connection Example (PHP is available on Linux hosting accounts only):
<?php
//Sample Database Connection Syntax for PHP and MySQL.
//Connect To Database
$hostname="your_mysqlserver.secureserver.net";
$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>";
}
}
?>
//Sample Database Connection Syntax for PHP and MySQL.
//Connect To Database
$hostname="your_mysqlserver.secureserver.net";
$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 php.net.