-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoopCrud.php
64 lines (47 loc) · 1.5 KB
/
oopCrud.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
53
54
55
56
57
58
59
60
61
62
63
64
<?php
class oopCrud{
private $host="localhost";
private $user="root";
private $db="students";
private $pass="";
private $conn;
public function __construct(){
$this->conn = new PDO("mysql:host=".$this->host.";dbname=".$this->db,$this->user,$this->pass);
}
public function showData($table){
$sql="SELECT * FROM $table";
$q = $this->conn->query($sql) or die("failed!");
while($r = $q->fetch(PDO::FETCH_ASSOC)){
$data[]=$r;
}
return $data;
}
public function getById($id,$table){
$sql="SELECT * FROM $table WHERE id = :id";
$q = $this->conn->prepare($sql);
$q->execute(array(':id'=>$id));
$data = $q->fetch(PDO::FETCH_ASSOC);
return $data;
}
public function update($id,$name,$email,$mobile,$address,$table){
$sql = "UPDATE $table
SET name=:name,email=:email,mobile=:mobile,address=:address
WHERE id=:id";
$q = $this->conn->prepare($sql);
$q->execute(array(':id'=>$id,':name'=>$name,':email'=>$email,':mobile'=>$mobile,':address'=>$address));
return true;
}
public function insertData($name,$email,$mobile,$address,$table){
$sql = "INSERT INTO $table SET name=:name,email=:email,mobile=:mobile,address=:address";
$q = $this->conn->prepare($sql);
$q->execute(array(':name'=>$name,':email'=>$email,':mobile'=>$mobile,':address'=>$address));
return true;
}
public function deleteData($id,$table){
$sql="DELETE FROM $table WHERE id=:id";
$q = $this->conn->prepare($sql);
$q->execute(array(':id'=>$id));
return true;
}
}
?>