Guest User

Untitled

a guest
Mar 28th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. <?php
  2. Class mysql
  3. {
  4.  
  5. public $query;
  6. public $data;
  7. public $result;
  8. public $rows;
  9. protected $config;
  10. protected $host;
  11. protected $port;
  12. protected $user;
  13. protected $pass;
  14. protected $dbname;
  15. protected $con;
  16.  
  17. public function __construct()
  18. {
  19. try
  20. {
  21. #array com dados do banco
  22. include 'database.conf.php';
  23. global $databases;
  24. $this->config = $databases['local'];
  25. # Recupera os dados de conexao do config
  26. $this->dbname = $this->config['dbname'];
  27. $this->host = $this->config['host'];
  28. $this->port = $this->config['port'];
  29. $this->user = $this->config['user'];
  30. $this->pass = $this->config['password'];
  31. # instancia e retorna objeto
  32. $this->con = new mysqli( "$this->host", "$this->user", "$this->pass","$this->dbname");
  33. //@mysql_select_db( "$this->dbname" );
  34. if ( !$this->con )
  35. {
  36. throw new Exception( "Falha na conexão MySql com o banco [$this->dbname] em database.conf.php" );
  37. }
  38. else
  39. {
  40. return $this->con;
  41. }
  42. }
  43. catch ( Exception $e )
  44. {
  45. echo $e->getMessage();
  46. exit;
  47. }
  48. return $this;
  49. }
  50.  
  51. public function query($query = '' )
  52. {
  53. try
  54. {
  55. if ( $query == '' )
  56. {
  57. throw new Exception( 'mysql query: A query deve ser informada como parâmetro do método.' );
  58. }
  59. else
  60. {
  61. $this->query = $query;
  62. $this->result = mysqli_query( $this->query );
  63. }
  64. }
  65. catch ( Exception $e )
  66. {
  67. echo $e->getMessage();
  68. exit;
  69. }
  70. return $this;
  71. }
  72.  
  73. public function fetchAll()
  74. {
  75. $this->data = "";
  76. $this->rows = 0;
  77. while ( $row = @mysqli_fetch_array( $this->result, MYSQLI_ASSOC ) )
  78. {
  79. $this->data[] = $row;
  80. }
  81. if ( isset( $this->data[0] ) )
  82. {
  83. $this->rows = count( $this->data );
  84. }
  85. return $this->data;
  86. }
  87.  
  88. public function rowCount()
  89. {
  90. return @mysqli_affected_rows();
  91. }
  92.  
  93. public function limit( $limit, $offset )
  94. {
  95. return "LIMIT " . ( int ) $limit . "," . ( int ) $offset;
  96. }
  97.  
  98. }
  99.  
  100. /* end file */
  101.  
  102. <?php
  103. global $databases;
  104. $databases = array(
  105. 'local' => array
  106. (
  107. 'host'=>'localhost',
  108. 'port'=>3306,
  109. 'dbname'=>'ok',
  110. 'user'=>'root',
  111. 'password'=>'root'
  112. )
  113. );
Add Comment
Please, Sign In to add comment