-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdo.php
52 lines (51 loc) · 1.36 KB
/
pdo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
class Database {
protected $db = array(
'rdbms' => 'mysql',
'host' => '127.0.0.1',
'port' => 3306,
'username' => 'dbUser',
'password' => 'dbPass'
);
var $link;
function __construct($dbName) {
try {
$this->link = new PDO(
$this->db['rdbms'].":host=".$this->db['host'].";dbname=".$dbName.";port=".$this->db['port'],
$this->db['username'],
$this->db['password']
);
$this->link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
echo $e->getMessage(); exit;
}
}
public function query($sql, $row = false) {
$res = Array();
$res['count'] = 0;
try {
$exec = $this->link->query($sql);
$res['sql'] = $sql;
$res['count'] = $exec->rowCount();
$views = array('select','show','describe');
$prefix = strtok(trim(preg_replace('/\PL/u',' ',strtolower($sql))),' ');
if(in_array($prefix,$views)) {
$res['cols'] = Array();
if($exec->columnCount() > 0) {
foreach(range(0, $exec->columnCount() - 1) as $columns) {
$meta = $exec->getColumnMeta($columns);
$res['cols'][] = $meta['name'];
}
}
$res['match'] = $exec->fetchAll(PDO::FETCH_ASSOC);
if(is_numeric($row)) return $res['match'][$row][$res['cols'][0]];
}
}
catch(PDOException $e) {
$res['error'] = $e->getMessage();
}
return $res;
}
}
?>