2

from the past few days i have been following a lot of tutorials regarding different ways to interact with database. like the old MySQL way, MySQLi, MySQL prepared statement, PHP's PDO etc.

i am still a new comer to the programming as it has been hardly 1 year since i learned and started coding. from time to time i have made lot of efforts on improving my codes and adhere to the standards as defined by the web developers. and now is the time i feel i change the way i am used to interact with the database using old MySQL way like mysql_connect() and mysql_query().

I have following configuration on my production machine.

  • Mac OSX 10.6 with MAMP installed
  • PHP version : 5.2.13
  • MySQL client version: 5.1.44

what do you think i should be using for database access and why?

  • MySQL
  • MySQLi
  • MySQL Prepared Statement
  • PDO
Ibrahim Azhar Armar
  • 25,288
  • 35
  • 131
  • 207

3 Answers3

3
  • Mysql ext is no more.
  • Assuming MySQL Prepared Statement is an SQL implementation, I find it hardly usable in PHP

which leaves us only two possible choices

  • PDO. If you are planning to use the database API as is, then PDO is the obvious winner, being much more user friendly than mysqli. Also, if you are writing an open-source library, then PDO is the only choice as it will let a user to connect any database supported. All in all, PDO makes a good rule of thumb: if you don't know which driver to use - go straight for PDO.
  • mysqli. If you are going to write a database wrapper to encapsulate raw API functions and your database backend will always remain mysql and named parameters is not a game changer for you, then mysqli could be a good choice as well, providing a few handy mysql-specific functions such as mysqli_info().
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
1

It really depends on what you are doing.

Prepared statements (via mysqli or PDO) can be useful if you are doing lots of high volume queries that all use the prepared statement. The prepared statements are faster, additionally you don't have to worry about things like escaping data with prepared statements but they take a bit of code overhead so for a simple app sometimes it's not worth using prepared statements and using the old mysql_* functions are easier for the intended purpose.

Jesse Cohen
  • 4,010
  • 22
  • 25
0

PDO for MySQL has several major advantages over directly using MySQLi.

  1. PDO supports more than one database backend. This helps should one of your clients request a port of your application to a different database.
  2. Prepared statements in PDO support named placeholders in addition to the positional (?) placeholders that MySQLi supports.
  3. PDO supports binding a single parameter at a time instead of all parameters at once. This allows stepping through an array of arguments and binding each, instead of having to construct a type string and then use call_user_func_array() black magic.
  4. PDO supports a convenient shortcut by passing an array to $stmt->execute(), where each key's value is bound to the placeholder as the key. (Caveat: It casts each value to a string, so you have to bind one at a time if your statement has a variable LIMIT. This is documented, and there's a feature request to change this casting to string.)

Items 2 through 4 combine to make it much easier to express the right side of operator IN in a provably injection-safe manner. With PDO, you can build an associative array with sequential names (such as [':likeval0'=>$val0, ':likeval1'=>$val1, ':likeval2'=>$val2]) and then safely build a placeholder list on the right side out of that array. Code might look like this:

$args = [];
foreach ($usernames as $n=>$value) {
    $args[":likeval$n"] = $value;
}
$list = implode(',', array_keys($args));
// result is like ':likeval0,:likeval1,:likeval2'
$stmt = "SELECT * FROM app_users WHERE `username` IN ($list)";
$stmt = $dbh->prepare($stmt);
$c = $stmt->execute($args);

MySQLi supports only ? placeholders and all-at-once binding through a variable argument function call. In MySQLi, constructing the right side of operator IN is actually a lot easier by using $dbh->escape_string() for each element in a list. This approach is safe against SQL injection if well tested but raises red flags with some "Bobby Tables" parameterization purists.

Community
  • 1
  • 1
Damian Yerrick
  • 4,602
  • 2
  • 26
  • 64