Last active
March 7, 2017 21:56
-
-
Save herloct/1e156ff8270f46d3604e01243a924252 to your computer and use it in GitHub Desktop.
Query OO
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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