Posts Tagged: MySQL


20
Aug 11

PHP one line prepared statements! Mysqli will never be the same again.

Mysqli was intended to be a wrapper around Mysql that would provide safe queries. You may wonder why this has not become the default way to write queries. Have a look at the exact same code written with mysqli.

// Mysqli Method

$mysqli = new mysqli('host', 'user', 'pass', 'database');

$query = "SELECT Name, CountryCode FROM City WHERE Zip = ? AND Population > ?";

if ($stmt = $mysqli->prepare($query)) {

$stmt->bind_param('si', $zip, $pop);

$stmt->execute();

$stmt->bind_result($name, $code);

while ($stmt->fetch()) {

// $name, $code

}

$stmt->close();

}

Yes, 10 lines to make a SELECT. Obviously they failed to design an easy to use API.

via Mysqli Wrapper – Short and Secure Queries | Vjeux.

Chris Chedeau is baller. I hate handling SQL queries in PHP more than anything else, it’s enough to make me stall progress. He put together a class that lets you run prepared statements and retrieve an associative array in one six character function. Get the code here.

Enhanced by Zemanta