View Full Version : Best way to communicate with a database?

03-11-2013, 01:26 AM
with mysql_query($query) being out-dated

whats the best way to query a database?

Mysqli, PDO etc?

and what are the advantages of each?

this is my current db.php:

function Q($query) {
$Username = 'root';
$Password = '***';
$Database = '***';
$Host = 'localhost';
global $queries, $queries_time, $queries_count;
mysql_connect($Host, $Username, $Password) or die ( 'Unable to connect to the server.<br>' );
mysql_select_db($Database) or die ( 'Unable to select the database.<br>' );
$queries .= 'Line <b>'. __LINE__ . '</b> of file <b>' .__FILE__ . '</b> requested <b>' . $query . '</b>';
$time_overall = round(microtime(),4);
if (!$result = mysql_query($query)) {
echo '<pre>Error on line <b>'. __LINE__ . '</b> of file <b>' . __FILE__ . '</b> requesting <b>' . $query . '</b></pre><br>';
$queries .= ' <b>ERROR OCCURED</b><br>';
} else {
$queries .= '<br>';
$time2_overall = round(microtime(), 4);
$gen_overall = $time2_overall - $time_overall;
$queries_time = substr($queries_time + $gen_overall,0,6) ;
mysql_close() or die ( 'Unable to disconnect from the server.<br>' );
return $result;

03-14-2013, 08:37 PM
I was hoping someone would gives some answers to this, because I'm on the fence with this also.
I went with MySqli because it's easy to convert from MySql. :) I have also read that PDO in php is buggy.

But PDO is the standard(or so I have been told) and I shall go to it as soon as I hear that it is not buggy. Also, PDO is object orientated so getting things from the db and into an object is easier. CLARIFY: I don't use objects to often so this statement is from hearsay, but will start when I learn PDO.

That's my 2cents. Hope others will post here.
Free bump.

Old Pedant
03-14-2013, 09:39 PM
Use ASP.NET? <grin/>

Sorry, I just couldn't help it.