Skip to content

Instantly share code, notes, and snippets.

@ardhiesta
Last active February 20, 2017 02:24
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 ardhiesta/1349b401479d38af477ee091b878e511 to your computer and use it in GitHub Desktop.
Save ardhiesta/1349b401479d38af477ee091b878e511 to your computer and use it in GitHub Desktop.
doing some SQL operation to get data from database
<?php
class WorldMapper extends Mapper{
// select semua data dari tabel country
public function getCountries(){
$sql = "SELECT Name, Continent, Region from country";
$stmt = $this->db->query($sql);
$results = [];
while($row = $stmt->fetch()) {
$results[] = array_map('utf8_encode', $row);
}
return $results;
}
// select data dari tabel country dengan pagination
public function getCountriesPaging($starts_record, $records_per_page){
$sql = "SELECT Name, Continent, Region from country limit ".$starts_record.", ".$records_per_page;
$stmt = $this->db->query($sql);
$results = [];
while($row = $stmt->fetch()) {
// array_map('utf8_encode', $row) digunakan agar hasil query dapat terbaca
// ketika nanti diformat ke dalam bentuk json mengingat database world menggunakan encoding selain utf8
// yang disebabkan adanya karakter-karakter unik untuk menyimpan nama negara
$results[] = array_map('utf8_encode', $row);
}
return $results;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment