-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdb.php
More file actions
35 lines (35 loc) · 899 Bytes
/
db.php
File metadata and controls
35 lines (35 loc) · 899 Bytes
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
<?php
function db_connect(){
$host = "127.0.0.1";
$user = "root";
$password = "";
$db = "wf_api";
try {
$conn = new PDO("mysql:host=".$host.";dbname=".$db.';charset=utf8', $user, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $conn;
}
catch(PDOException $e) {
die($e->getMessage());
}
}
function query( $sql, $params=array()){
$conn = db_connect();
$query = $conn->prepare($sql);
if(!empty($params)){
$query->execute($params) or die ('Internal error');
}else{
$query->execute() or die ('Internal error');
}
return $query;
}
function fetch_all( $sql, $params=array() ){
$query = query($sql, $params);
try{
$results = $query->fetchAll(PDO::FETCH_ASSOC);
}catch (Exception $e){
return $e->getMessage();
}
return $results;
}
?>