NYCPHP Meetup

NYPHP.org

[nycphp-talk] PDO or sprintf for creating sql queries.

John Campbell jcampbell1 at gmail.com
Fri Nov 9 20:48:36 EST 2007


>  Was anyone else completely annoyed by the way most
> of the params were switched between mysql and mysqli where the db link was
> required and put as the first param in most functions?

Nah... just use the object notation, and it actually makes more sense.

*new way*
$db_conn = new mysqli("localhost", "my_user", "my_password", "world");
$cursor = $db_conn->query("SELECT first_name FROM Users LIMIT 0,10");
while($row = $cursor->fetch_assoc()) {
  echo $row['first_name'];
}
$cursor->close();

*old way*
$link = mysql_connect("localhost", "my_user", "my_password", "world");
$cursor = mysql_query("SELECT first_name FROM Users LIMIT 0,10");
while($row = mysql_fetch_assoc($cursor)) {
  echo $row['first_name'];
}
mysql_free_result($cursor);

There is slightly less typing with the new way, and it will work with
mutiple connections. :)

Regards,
John Campbell



More information about the talk mailing list