Skip to content

Instantly share code, notes, and snippets.

@igoralves1
Created December 5, 2016 16:03
Show Gist options
  • Save igoralves1/0244b01dbdee852dc700724aab1ec47c to your computer and use it in GitHub Desktop.
Save igoralves1/0244b01dbdee852dc700724aab1ec47c to your computer and use it in GitHub Desktop.
API class to grab info in the database using a sql query sent trough the URL
//API
public static function get_list($table,$displayType,$fields=NULL,$complement=NULL) {//Default = JSON
try {
$db=new db();
if(isset($fields) && isset($complement)){
$db->sql = "SELECT $fields FROM $table $complement;";
}
else{
$db->sql = "SELECT * FROM $table;";
}
//die($db->sql);
$stmt = $db->conn->prepare($db->sql);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_NAMED);
//die($displayType);
if($displayType=="json"){
return json_encode($result);
}
else{
return print_r($result);
}
}// End of try
catch(PDOException $e){
echo $e->getMessage();
$this->conn = null;
die();
}// End of catch
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment