-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.php
executable file
·99 lines (75 loc) · 2.73 KB
/
db.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
/**
* @author Nicholas Sardo
*
* Quick and crude SQLite Class to handle Database for Flight Rest Reference Impl.
*/
class DB {
private $db;
private function __construct(){
$this->db = new PDO( 'sqlite:School.db' );
if ( !$this->db ) die ( $error );
return $this->get_db();
}
private function __clone(){}
private function __wakeup(){}
function get_db(){
return $this->db;
}
public static function get_instance(){
static $instance = null;
if ( (is_null( $instance )) )
$instance = new DB();
return $instance;
}
function quer( $id ){
$stmt = $this->db->prepare( "SELECT * from Students WHERE id= :id" );
$stmt->bindParam( ':id', $id, PDO::PARAM_INT );
$stmt->execute();
$result = $stmt->fetchAll();
foreach( $result as $row){
return array( 'id' => $row['id'], 'full_name' => $row['full_name'],
'school_name' => $row['school_name'], 'phone' => $row['phone'],
'email' => $row['email'] );
}
}
function createContactInfo( $js ){
$ar = json_decode( $js, true );
extract( $ar[0] );
#(null,"moe","eschool","12334","em@the")
$sql = "INSERT INTO Students( id,full_name, school_name, phone, email ) VALUES ( null,'$full_name', '$school_name', '$phone', '$email' )";
$this->db->exec( $sql );
}
function updateGrade( $id, $course, $grade ){
$stmt = $this->db->prepare( "Update Courses set grade= :grade where student_id= :id and name= :course" );
$stmt->bindParam( ':id', $id, PDO::PARAM_INT );
$stmt->bindParam( ':course', $course, PDO::PARAM_STR, 128 );
$stmt->bindParam( ':grade', $grade, PDO::PARAM_STR, 128 );
$stmt->execute();
}
function createStudentCourse( $id, $course, $grade, $start_date, $end_date ){
$stmt = "INSERT INTO Courses ( id, student_id, name, start_date, end_date, grade ) VALUES ( null, '$id', '$course', '$start_date', '$end_date', '$grade' ";
$this->db->exec( $stmt );
}
function getGrade( $id, $course ){
$stmt = $this->db->prepare( "SELECT * FROM Courses WHERE student_id = :id and name = :course LIMIT 1" );
$stmt->bindParam( ':id', $id, PDO::PARAM_INT );
$stmt->bindParam( ':course', $course, PDO::PARAM_STR, 128 );
$stmt->execute();
$result = $stmt->fetchAll();
foreach( $result as $row ){
return $row['grade'];
}
}
function getStudentCourses( $id ){
$a = array();
$stmt = $this->db->prepare( "SELECT * FROM Courses WHERE student_id = :id" );
$stmt->bindParam( ':id', $id, PDO::PARAM_INT );
$stmt->execute();
$result = $stmt->fetchAll(); #fetch(PDO::FETCH_OBJ); #fetch(); #fetch(PDO::FETCH_ASSOC); #fetchAll(PDO::FETCH_ASSOC);
foreach( $result as $row ){
$a[] = array( 'course' => $row['name'], 'grade' => $row['grade'], 'start_date' => $row['start_date'], 'end_date' => $row['end_date'] );
}
return $a;
}
}