Register now or log in to join your professional community.
OH ENOUGH WITH THE SILLY QUESTIONS !!!
Why not use a framework such as Symfony, CodeIgnitor or zend. Will make your life much easier with trivial tasks such as connecting to a database.
You can choose between
mysql_connect('server_address', 'db_user', 'db_password');
or
mysqli_connect('server_address', 'db_user', 'db_password', 'db_name');
the difference between mysql and mysqli that mysqli improved to allow developers to use all new features in mysql database4.1 and higher, plus it has been implemented in OOP.
for any further information you can check the manual of PHP and MySQL integration from here
<?php
function get_db_connection ( $server="localhost" , $user="root" , $password="root" , $database="MY_DB") {
$dbhandle = mysql_connect($server, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
$selected = mysql_select_db($database,$dbhandle)
or die("Could not select examples");
mysql_close($dbhandle);
}
?>
$username = 'root'; // Please replace here your database username
$password = '';// Please replace here your database password
$hostname = 'localhost'; // Please replace here your database hostname
$dbname = 'example'; // Please replace here your database name
$connection = mysql_connect( $hostname, $username, $password );
mysql_select_db($dbname, $connection);
addional to the prev answer you can conect to the data base using PDO Methedolgy (PHP Data Objects).
which is defines a lightweight, consistent interface for accessing databases.
PDO provides a data-access abstraction layer, which means that, regardless of which database you're using, you use the same functions to issue queries and fetch data.
Click Here for more information
mysql_* function are deprecated and are no longer maintained. So it would be nice to use either mysqli or PDO. Using the procedural style of both won't benifit much. The syntax are given below:
Mysqli
$mysqli = new mysqli("hostName", "username", "password", "db_name");
if ($mysqli->connect_error) {
die('Connect Error: ' . $mysqli->connect_error);
}
PDO
try {
$pdo = new PDO('mysql:host=hostName;dbname=your_db_name', 'username', 'password');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}