Monday, February 14, 2011

Mysqli VS PDO

The reason i prefer PDO is it supports both binding named parameters and binding positioned parameters. Mysqli, on the other hand, only supports binding positioned parameters.

1.Mysqli:
$sql = "select * from user where id > ? and id < ?";
$bind = array(1, 10);

As a developer, you must ensure your $bind array elements are in correct position. For the above example, if your $bind array is array(10, 1), the query will actually become "select * from user where id > 10 and id < 1)".

2.PDO(Mysql):
$sql = "select * from user where id > :start and id < :end";
$bind = array("end"=>10, "start"=>1);

My opinion is binding named parameters really improves the program's readability.

No comments: