Summary

Learn how to connect to a MySQL database using mysql_connect() and mysql_select_db() PHP functions.

Before you start checklist

Make sure you’ve done the following steps before you start:

  1. Your Ecenica account must support MySQL and PHP.
  2. You have created created a MySQL database.
  3. Your MySQL database name, username and password. See What are my MySQL Settings?

Example

The following example opens a connection to a MySQL server, selects a MySQL database and then closes the connection.

<?php
//Set MySQL Configuration Settings
$mysql_server='localhost';
$my_db='database';
$mysql_user='username';
$mysql_password='password';

//Connect to MYSQL
$con = mysql_connect($mysql_server, $mysql_user, $mysql_password);
if (!$con) {
    die('Could not connect: ' . mysql_error());
}

//Select a database
db_selected=mysql_select_db("my_db", $con);
if (!$db_selected) {
    die (''Could not select database : ' . mysql_error());
}

//Some code
echo "Congratulations - you've connected to your database!"

//Close connection
mysql_close($con);
?>

External links