Advertisement
Guest User

Untitled

a guest
Mar 16th, 2017
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. <?php
  2. class database
  3. {
  4. private $host = 'localhost';
  5. private $dbName = 'phppdotest';
  6. private $user = 'root';
  7. private $pass = '';
  8.  
  9. private $dbh;
  10. private $error;
  11. private $qError;
  12.  
  13. private $stmt;
  14.  
  15. public function __construct()
  16. {
  17. //dsn for mysql
  18. $dsn = "mysql:host=" . $this->host . ";dbname=" . $this->dbName;
  19. $options = array(
  20. PDO::ATTR_PERSISTENT => true,
  21. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
  22. );
  23.  
  24. try {
  25. $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
  26. }
  27. catch (PDOException $e) {
  28. $this->error = $e->getMessage();
  29. }
  30.  
  31. }
  32.  
  33. public function query($query)
  34. {
  35. $this->stmt = $this->dbh->prepare($query);
  36. }
  37.  
  38. public function bind($param, $value, $type = null)
  39. {
  40. if (is_null($type)) {
  41. switch (true) {
  42. case is_int($value):
  43. $type = PDO::PARAM_INT;
  44. break;
  45. case is_bool($value):
  46. $type = PDO::PARAM_BOOL;
  47. break;
  48. case is_null($value):
  49. $type = PDO::PARAM_NULL;
  50. break;
  51. default:
  52. $type = PDO::PARAM_STR;
  53. }
  54. }
  55. $this->stmt->bindValue($param, $value, $type);
  56. }
  57.  
  58. public function execute()
  59. {
  60. return $this->stmt->execute();
  61.  
  62. $this->qError = $this->dbh->errorInfo();
  63. if (!is_null($this->qError[2])) {
  64. echo $this->qError[2];
  65. }
  66. echo 'done with query';
  67. }
  68.  
  69. public function resultset($type = false)
  70. {
  71. $this->execute();
  72. if (!$type) {
  73. return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
  74. }else{
  75. return $this->stmt->fetchAll(PDO::FETCH_OBJ);
  76. }
  77. }
  78. public function single($type = false)
  79. {
  80. $this->execute();
  81. if (!$type) {
  82. return $this->stmt->fetch(PDO::FETCH_ASSOC);
  83. }else{
  84. return $this->stmt->fetch(PDO::FETCH_OBJ);
  85. }
  86. }
  87.  
  88. public function rowCount()
  89. {
  90. return $this->stmt->rowCount();
  91. }
  92.  
  93. public function lastInsertId()
  94. {
  95. return $this->dbh->lastInsertId();
  96. }
  97.  
  98. public function beginTransaction()
  99. {
  100. return $this->dbh->beginTransaction();
  101. }
  102.  
  103. public function endTransaction()
  104. {
  105. return $this->dbh->commit();
  106. }
  107.  
  108. public function cancelTransaction()
  109. {
  110. return $this->dbh->rollBack();
  111. }
  112.  
  113. public function debugDumpParams()
  114. {
  115. return $this->stmt->debugDumpParams();
  116. }
  117.  
  118. public function queryError()
  119. {
  120. $this->qError = $this->dbh->errorInfo();
  121. if (!is_null($qError[2])) {
  122. echo $qError[2];
  123. }
  124. }
  125. }
  126.  
  127. $db = new database();
  128.  
  129. if (isset($_GET['a'])) {
  130. if ($_GET['a'] == 'show') {
  131. $db->query('SELECT * FROM users');
  132. $rows = $db->resultset();
  133.  
  134. foreach ($rows as $row) {
  135. echo "Name ".$row['name']." Age ".$row['age']." Sex ".$row['sex']."<br>";
  136. }
  137. }elseif ($_GET['a'] == 'insert') {
  138. $db->query('INSERT INTO users (name, age, sex) VALUES(:name, :age, :sex)');
  139. $db->bind(':name', 'Aboo Thahir 1');
  140. $db->bind(':age', '24');
  141. $db->bind(':sex', 'Male');
  142. $db->execute();
  143. header('Location: phppdo.php?a=show');
  144. }
  145. }
  146. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement