Guest User

Untitled

a guest
Jul 22nd, 2018
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. Pesquisa retornou 1 resultados
  2. array(1) {
  3. [0]=>
  4. array(16) {
  5. ["id"]=>
  6. string(1) "1"
  7. [0]=>
  8. string(1) "1"
  9. ["name"]=>
  10. string(17) "Lucas de Carvalho"
  11. [1]=>
  12. string(17) "Lucas de Carvalho"
  13. ["email"]=>
  14. string(27) "contato@lucasdecarvalho.com"
  15. [2]=>
  16. string(27) "contato@lucasdecarvalho.com"
  17. ["username"]=>
  18. string(15) "lucasdecarvalho"
  19. [3]=>
  20. string(15) "lucasdecarvalho"
  21. ["registred"]=>
  22. NULL
  23. [4]=>
  24. NULL
  25. ["password"]=>
  26. string(23) "12312312312312312312312"
  27. [5]=>
  28. string(23) "12312312312312312312312"
  29. ["status"]=>
  30. string(1) "0"
  31. [6]=>
  32. string(1) "0"
  33. ["registration_key"]=>
  34. string(13) "muasdsadaosdj"
  35. [7]=>
  36. string(13) "muasdsadaosdj"
  37. }
  38. }
  39.  
  40. <!DOCTYPE html>
  41. <html lang="pt-br">
  42. <head>
  43. <meta charset="UTF-8">
  44. <title>Livre e Leve</title>
  45. </head>
  46. <body>
  47. <pre>
  48. <?php
  49. require 'vendor/autoload.php';
  50. require '_app/config.php';
  51.  
  52. use LelvtexConnRead;
  53.  
  54. $a = new Read;
  55. $a->exeRead('cmslcs_users');
  56.  
  57. echo "Pesquisa retornou {$a->getRowCount()} resultados";
  58.  
  59. echo "<hr>";
  60.  
  61. var_dump($a->getReadResult());
  62.  
  63. ?>
  64. </pre>
  65. </body>
  66. </html>
  67.  
  68. <?php
  69.  
  70. namespace LelvtexConn;
  71.  
  72. class Read extends Conn
  73. {
  74.  
  75. private $conn;
  76. private $query;
  77. private $result;
  78. private $table;
  79. private $read;
  80. private $terms;
  81. private $links;
  82.  
  83. public function exeRead($table, $terms = null, $links = null)
  84. {
  85.  
  86. if($terms) {
  87. $this->terms = htmlspecialchars(trim(strip_tags($terms)));
  88. }
  89.  
  90. if (!empty($links)){
  91. parse_str($links, $this->links);
  92. }
  93.  
  94.  
  95. $this->table = htmlspecialchars(trim(strip_tags($table)));
  96. $this->execRead();
  97. }
  98.  
  99.  
  100. public function getReadResult()
  101. {
  102. return $this->result;
  103. }
  104.  
  105. /*
  106. * O rowCount verifica quantos registros existem na database
  107. */
  108.  
  109. public function getRowCount()
  110. {
  111. if ($this->read->rowCount()) {
  112. return $this->read->rowCount();
  113. }
  114. }
  115.  
  116. /*
  117. * Prepara a query, puxando a conexão da Class Conn
  118. */
  119.  
  120. private function prepareQuery()
  121. {
  122. $this->query = "SELECT * FROM {$this->table} {$this->terms}";
  123. $this->conn = parent::getConn();
  124. $this->read = $this->conn->prepare($this->query);
  125.  
  126. }
  127.  
  128. /*
  129. * Faz o bind value nos links :meulink se o $links for preenchido la em cima no exeRead
  130. */
  131.  
  132. private function formatLnks(){
  133. if($this->links){
  134. foreach ($this->links as $link => $val) {
  135. if($link == "limit" || $link == "offset"){
  136. $val = (int) $val;
  137. }
  138. $this->read->bindValue(":{$link}", $val, ( is_int($val) ? PDO::PARAM_INT : PDO::PARAM_STR));
  139. }
  140. }
  141. }
  142.  
  143. /*
  144. * Executa toda a função. primeiro ele prepara a query com o prepareQuery, depois dentro de um try catch, ele termina o que deve ser executado.
  145. */
  146.  
  147. private function execRead()
  148. {
  149. $this->prepareQuery();
  150. try {
  151. $this->formatLnks();
  152. $this->read->execute();
  153. $this->result = $this->read->fetchAll();
  154. } catch (PDOException $e) {
  155. $this->result = null;
  156. echo "Erro na linha <b> {$e->getLine()} </b> no arquivo {$e->getFile()}: " . $e->getMessage();
  157. }
  158. }
  159.  
  160. }
Add Comment
Please, Sign In to add comment