Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

How do you connect mysql database with PHP ?

user-image
Question ajoutée par Ravi Kumar Ravi
Date de publication: 2014/01/16
Mohammad Chehab
par Mohammad Chehab , Digital Project Manager | Head Of Mobile & Web , Mohammed Bin Rashid Housing Est

OH ENOUGH WITH THE SILLY QUESTIONS !!!

Mahmoud Ghazal
par Mahmoud Ghazal , Senior Agile Project Manager , Telstra

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.

Mo'ath Al Mallahi
par Mo'ath Al Mallahi , Senior PHP Developer , iHorizons

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

 

Muhammad Majid Saleem
par Muhammad Majid Saleem , Senior PHP Developer / Project Manager , SwaamTech

  You can see its documentation here: http://pk1.php.net/manual/en/function.mysql-connect.php   <?php$link mysql_connect('localhost''mysql_user''mysql_password');if (!$link) {    die('Could not connect: ' mysql_error());}echo 'Connected successfully';mysql_close($link);?>

abdulla abu zakham
par abdulla abu zakham , PHP / Drupal Developer , vardot

<?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);

}

?>

Muhammad Abid Saleem
par Muhammad Abid Saleem , Software Engineer , PVT Company LTD lahore

$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);

Abdullah Mohammed
par Abdullah Mohammed , Senior Software Engineer PHP & PERL , CASHU PAYMENT ONLINE SOLUTION

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

Nouphal Muhammed
par Nouphal Muhammed , Senior Web Developer , Planet Green Solutions

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();

 

}

 

 

More Questions Like This