Register now or log in to join your professional community.
Approach1 (): $mysqli = new mysqli("$mysql_server", "$mysql_user", "$mysql_pw", "$mysql_db"); if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; } Approach2 $link = mysqli_connect("$mysql_server", "$mysql_user", "$mysql_pw", "$mysql_db"); if (!$link) { die('Could not connect: ' . mysql_error()); }
The better solution is to create a PHP class to manage all the connection aspect, after that u call this classe anywhere if you want to establish a connection , i will give you here an example of a class, i made this class, its very useful for me....
class connection
{
var $host; // ip ou bien adresse
var $database;
var $user;
var $password;
//le constructeur:
function connection ($host,$base,$utilisateur,$pwd)
{
$this->host=$host;
$this->database=$base;
$this->user=$utilisateur;
$this->password=$pwd;
}//function connection ($base,$utilisateur,$pwd)
//c'est une fonction qui etablit une connection avec le serveur: mysql, cette fonction reourne: link avec
//le serveur: mysql:
function connecter ()
{
/* Connecting, selecting database */
$link = mysql_pconnect($this->host,$this->user,$this->password);
//echo $this->host.'<p>'.$this->user.'<p>'.$this->password;
if (!($link)) $compteur=1;
while ( (!($link)) && ($compteur<=10) )
{
$link = mysql_pconnect($this->host,$this->user,$this->password);
$compteur++;
}
if ( ($compteur>10) && (!($link)) ) die("<p align=\"right\">Could not connect to the data base : " . mysql_error());
return ($link);
}//function connecter ()
// c'est la meme fonction precedente, mais, elle utilise: connect au lieu de pconnect, et elle passe l'argument: true a la fonction connect
// pour permettre l'ouverture de plusieurs links en meme temps dans le meme script
function connecter_multi ()
{
/* Connecting, selecting database */
$link = mysql_connect($this->host,$this->user,$this->password,true);
if (!($link)) $compteur=1;
while ( (!($link)) && ($compteur<=10) )
{
$link = mysql_connect($this->host,$this->user,$this->password,true);
$compteur++;
}
if ( ($compteur>10) && (!($link)) ) die("<p align=\"right\">Could not connect to the data base : " . mysql_error());
return ($link);
}//function connecter_multi ()
//c'est une fonction qui ferme une connection donnée en argument:
function fermerConnection($link)
{
/* Closing connection */
mysql_close($link);
}//function fermerConnection()
//c'est une fonction qui etablit une connection avec la base de donnees: $database:
function connecterBDD($link='')
{
if (empty($link))
mysql_select_db($this->database) or die("<p align=\"right\">Could not select database: ".$this->database.' error: '.mysql_error());
else
mysql_select_db($this->database,$link) or die("<p align=\"right\">Could not select database: ".$this->database.' error: '.mysql_error());
}//function connecterBDD()
//c'est une fonction qui prend en argument une requete: SQL et execute cette requete
//sur la connection etablie, cette fontion retourne : $result
function executer ($requete,$link='')
{
//echo $requete."<p>";
if($link=='')
$result = mysql_query($requete) or die($requete);
else
$result = mysql_query($requete,$link) or die($requete);
if ( mysql_errno !=0 ) {
// Check the connection
// No connection
// Raise a flag
// Connection found
// Continue normally
die("<p align=\"right\">Query failed : " . mysql_error());
}
return ($result);
}//function executer ($requete)
function ouvrir_connection()
{
$newConnection=new connection("localhost","jo","root","");
$link=$newConnection->connecter();
$newConnection->connecterBDD();
}//function ouvrir_connection()
// connexion vers un serveur
function ouvrir_connection_serveur($host,$database,$username,$password)
{
$newConnection=new connection($host,$database,$username,$password);
$link=$newConnection->connecter();
$newConnection->connecterBDD($link);
return($link);
}//function ouvrir_connection_serveur($host,$database,$username,$password)
// c'est la meme fonction precedente, mais elle permet l'ouverture de plusieurs links en meme temps dans le meme script
function ouvrir_connection_serveur_multi($host,$database,$username,$password)
{
$newConnection=new connection($host,$database,$username,$password);
$link=$newConnection->connecter_multi();
$newConnection->connecterBDD($link);
return($link);
}//function ouvrir_connection_serveur_multi($host,$database,$username,$password)
//c'est une fonction qui etablit une connection avec la base de donnees: $database:
function connecterBDD_link($link)
{
mysql_select_db($this->database,$link) or die("<p align=\"right\">Could not select database: ".$this->database.' error: '.mysql_error());
}//function connecterBDD()
function ouvrir_connection_serveur_link($host,$database,$username,$password)
{
$newConnection=new connection($host,$database,$username,$password);
$link=$newConnection->connecter();
$newConnection->connecterBDD_link($link);
return($link);
}//function ouvrir_connection_serveur($host,$database,$username,$password)
}//class connection
Both are same in my point of view but the differennce is:
Appache1 is using MYSQLI whereas Appache2 is uing simple MYSQL.
Brother ...
Use any approch until unless you know definate requirements. But I sugguest to use singleton pattren for sql connection to make single connection when ever you need to hit database which over come the hits and maintain the code too.
simple example : [copy from http://stackoverflow.com/questions/4477655/using-singleton-to-create-single-mysqli-connection-and-using-it-across-multiple]
publicstaticfunctionGet(){if(!self::$dbLink){self::$dbLink =new mysqli(DB_HOST, DB_USER, DB_PWD, DB_NAME);if(mysqli_connect_errno()){thrownewException("Database connection failed: ".mysqli_connect_error());}//was here}returnself::$dbLink;//should be here}
It's the same think, the second approach with mysqli_connect it just an alias of new mysqli.
Reference php.net
Approach2
كل ده ومش لاقى شغل الله يبشرك بالخير دا انا لسه مدخلتش فال oop وكنت متفائل حتى المبرمجين مش لاقين شغل
First method is pure OOP style and second is procedural style, technically there not much difference you can use any one at your ease, but its better to make separate DB class and use that in your code, so that you can always change db and their respective methods easily.
but in php the Best is to use framework for application development like symfony, cake etc.
Both are correct. If you are programming with object oriented paradigm you can use Approach1 as it is using the object oriented approach. In pocedural programming use Approach2,as it is the procedural style.
Cheers!
Approach one, OOP based.