· 10 years ago · Sep 22, 2016, 07:42 PM
1/**
2 * @return bool
3 */
4 private function create()
5 {
6 try{
7 $table = "CREATE TABLE IF NOT EXISTS `test` (
8 `id` INT NOT NULL AUTO_INCREMENT,
9 `script_name` VARCHAR(25) NOT NULL ,
10 `start_time` INT NOT NULL ,
11 `end_time` INT NOT NULL ,
12 `result` ENUM('normal','illegal','failed','success') NOT NULL,
13 PRIMARY KEY (`id`)
14 ) ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_unicode_ci;";
15 $this->db->exec($table);
16 }catch (PDOException $e){
17 echo $e->getMessage();
18 return false;
19 }
20 }
21
22 /**
23 * @return bool|PDOStatement
24 */
25 private function fill()
26 {
27 try {
28 $sql = "INSERT INTO `test` (script_name, start_time, end_time, result)
29 VALUES (:script_name, :start_time, :end_time, :result)";
30 $stmt = $this->db->prepare($sql);
31 $stmt->bindParam(':script_name', $this->randomString(), PDO::PARAM_STR);
32 $stmt->bindParam(':start_time', time(), PDO::PARAM_INT);
33 $stmt->bindParam(':end_time', time(), PDO::PARAM_INT);
34 $stmt->bindParam(':result', $this->randEnum(['normal', 'illegal', 'failed', 'success']), PDO::PARAM_STR);
35 $stmt->execute();
36
37 }catch (PDOException $e){
38 echo $e->getMessage();
39 return false;
40 }
41 return $stmt;
42 }
43
44 /**
45 * @return bool
46 */
47 public function get()
48 {
49 try{
50 $stmt = $this->db->prepare("SELECT id, script_name, start_time, end_time, result FROM `test` WHERE result IN (?, ?)");
51 $stmt->execute(['normal', 'success']);
52
53 while ($row = $stmt->fetch(PDO::FETCH_LAZY))
54 {
55 echo $row['id'] . " | " .$row['script_name'] ." | ".$row['start_time'] ." | " .$row['end_time'] . " | " .$row['result'] ."<br />";
56 }
57 }catch (PDOException $e){
58 echo $e->getMessage();
59 return false;
60 }
61 }