Skip to content

Instantly share code, notes, and snippets.

@herloct
Last active March 7, 2017 21:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save herloct/1e156ff8270f46d3604e01243a924252 to your computer and use it in GitHub Desktop.
Save herloct/1e156ff8270f46d3604e01243a924252 to your computer and use it in GitHub Desktop.
Query OO
<?php
require __DIR__.'/Query.php';
$mysql = new PDO('mysql:host=localhost;dbname=world', 'root', '');
$threeCountry = new Query($mysql, 'SELECT * FROM country LIMIT 3');
$countryByCode = new Query($mysql, 'SELECT * FROM country WHERE code = :code');
// --
$result = $threeCountry->execute();
while ($country = $result->fetch()) {
print_r($country);
}
$result = $countryByCode->execute([
':code' => 'IDN'
]);
$country = $result->fetch();
print_r($country);
// -- panggil lagi
$result = $threeCountry->execute();
while ($country = $result->fetch()) {
print_r($country);
}
$result = $countryByCode->execute([
':code' => 'IDN'
]);
$country = $result->fetch();
print_r($country);
<?php
class Query
{
private $pdo;
private $sql;
public function __construct(PDO $pdo, $sql)
{
$this->pdo = $pdo;
$this->sql = $sql;
}
public function execute(array $data = [])
{
$result = $this->pdo->prepare($this->sql);
$result->execute($data);
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment