Instantly share code, notes, and snippets.
Last active Mar 27, 2017
This file contains 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_once 'config.php'; // Database setting constants [DB_HOST, DB_NAME, DB_USERNAME, DB_PASSWORD] | |
class dbHelper { | |
private $db; | |
private $err; | |
function __construct() { | |
$dsn = 'mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset=utf8'; | |
try { | |
$this->db = new PDO($dsn, DB_USERNAME, DB_PASSWORD, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); | |
} catch (PDOException $e) { | |
$response["status"] = "error"; | |
$response["message"] = 'Connection failed: ' . $e->getMessage(); | |
$response["data"] = null; | |
//echoResponse(200, $response); | |
exit; | |
} | |
} | |
function select($table, $columns, $where, $order){ | |
try{ | |
$a = array(); | |
$w = ""; | |
foreach ($where as $key => $value) { | |
$w .= " and " .$key. " like :".$key; | |
$a[":".$key] = $value; | |
} | |
$stmt = $this->db->prepare("select ".$columns." from ".$table." where 1=1 ". $w." ".$order); | |
$stmt->execute($a); | |
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC); | |
if(count($rows)<=0){ | |
$response["status"] = "warning"; | |
$response["message"] = "No data found."; | |
}else{ | |
$response["status"] = "success"; | |
$response["message"] = "Data selected from database"; | |
} | |
$response["data"] = $rows; | |
}catch(PDOException $e){ | |
$response["status"] = "error"; | |
$response["message"] = 'Select Failed: ' .$e->getMessage(); | |
$response["data"] = null; | |
} | |
return $response; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment