Advertisement
Guest User

Untitled

a guest
Aug 7th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.76 KB | None | 0 0
  1. global $databases;
  2. $databases = array(
  3. 'local' => array
  4. (
  5. 'host'=>'localhost',
  6. 'port'=>3306,
  7. 'dbname'=>'noticiass',
  8. 'user'=>'root',
  9. 'password'=>''
  10. )
  11. );
  12.  
  13. Class mysql
  14. {
  15.  
  16. public $query;
  17. public $data;
  18. public $result;
  19. public $rows;
  20. public $page = 0;
  21. public $perpage = 10;
  22. public $current = 1;
  23. public $url;
  24. public $link = '';
  25. public $total = '';
  26. public $pagination = false;
  27.  
  28. protected $config;
  29. protected $host;
  30. protected $port;
  31. protected $user;
  32. protected $pass;
  33. protected $dbname;
  34. protected $con;
  35.  
  36.  
  37.  
  38. public function __construct()
  39. {
  40. try
  41. {
  42. #array com dados do banco
  43. include 'database.conf.php';
  44. global $databases;
  45. $this->config = $databases['local'];
  46. # Recupera os dados de conexao do config
  47. $this->dbname = $this->config['dbname'];
  48. $this->host = $this->config['host'];
  49. $this->port = $this->config['port'];
  50. $this->user = $this->config['user'];
  51. $this->pass = $this->config['password'];
  52. # instancia e retorna objeto
  53. $this->con = mysql_connect( "$this->host", "$this->user", "$this->pass" );
  54. mysql_select_db( "$this->dbname" );
  55. if ( !$this->con )
  56. {
  57. throw new Exception( "Falha na conexão MySql com o banco [$this->dbname] em database.conf.php" );
  58. }
  59. else
  60. {
  61. return $this->con;
  62. }
  63. $this->url = $_SERVER['SCRIPT_NAME'];
  64. }
  65. catch ( Exception $e )
  66. {
  67. echo $e->getMessage();
  68. exit;
  69. }
  70. return $this;
  71. }
  72.  
  73. public function query( $query = '' )
  74. {
  75. try
  76. {
  77. if ( $query == '' )
  78. {
  79. throw new Exception( 'mysql query: A query deve ser informada como parâmetro do método.' );
  80. }
  81. else
  82. {
  83. $this->query = $query;
  84. if($this->pagination == true){
  85. $this->result = mysql_query( $this->query );
  86. $this->fetchAll();
  87. $this->paginateLink();
  88. $this->query .= " LIMIT $this->page, $this->perpage";
  89. $this->pagination = false;
  90.  
  91. }
  92. $this->result = mysql_query( $this->query );
  93. }
  94. }
  95. catch ( Exception $e )
  96. {
  97. echo $e->getMessage();
  98. exit;
  99. }
  100. return $this;
  101. }
  102.  
  103. public function fetchAll()
  104. {
  105. $this->data = "";
  106. $this->rows = 0;
  107. while ( $row = mysql_fetch_array( $this->result, MYSQL_ASSOC ) )
  108. {
  109. $this->data[] = $row;
  110. }
  111. if ( isset( $this->data[0] ) )
  112. {
  113. $this->rows = count( $this->data );
  114. }
  115. return $this->data;
  116. }
  117.  
  118. public function rowCount()
  119. {
  120. return @mysql_affected_rows();
  121. }
  122.  
  123. public function getUrl($perpage)
  124. {
  125. $this->url = $_SERVER['REQUEST_URI'];
  126. return $this;
  127. }
  128. public function paginate($perpage)
  129. {
  130. $this->pagination = true;
  131. $this->perpage = $perpage;
  132. return $this;
  133. }
  134. public function paginateLink()
  135. {
  136. if(!preg_match('/?/',$this->url))
  137. {
  138. $this->url .= "?";
  139. }else{
  140. $this->url .= "&";
  141. }
  142. if ( isset( $_GET['page'] ) )
  143. {
  144. $this->current = $_GET['page'];
  145. $this->page = $this->perpage * $_GET['page'] - $this->perpage;
  146. if ( $_GET['page'] == 1 )
  147. {
  148. $this->page = 0;
  149. }
  150. }
  151. $this->total = $this->rows;
  152. if ( $this->rows > $this->perpage )
  153. {
  154. $this->link = "<div class="pagination"><ul>";
  155. $prox = "javascript:;";
  156. $ant = "javascript:;";
  157. if ( $this->current >= 2 )
  158. {
  159. $ant = $this->url."page=" . ($this->current - 1);
  160. }
  161. if ( $this->current >= 1 && $this->current < ($this->total / $this->perpage))
  162. {
  163. $prox = $this->url."page=" . ($this->current + 1);
  164. }
  165. $this->link .= '<li><a href="' . $ant . '">&laquo;</a></li>';
  166. $from = round( $this->total / $this->perpage );
  167. if($from == 1){$from++;}
  168.  
  169. for ( $i = 1; $i <= $from ; $i++ )
  170. {
  171. if ( $this->current == $i )
  172. {
  173. $this->link .= "<li class="active"><a>$i</a></li>n";
  174. }
  175. else
  176. {
  177. $this->link .= "<li><a href="".$this->url."page=$i">$i</a></li>n";
  178. }
  179. }
  180. $this->link .= '<li><a href="' . $prox . '">&raquo;</a></li>';
  181. $this->link .= "</ul>n";
  182. $this->link .= "</div>n";
  183. }
  184. return $this;
  185. }
  186.  
  187. public function cut($str,$chars,$info= '')
  188. {
  189. if ( strlen( $str ) >= $chars )
  190. {
  191. $str = preg_replace( '/ss+/', ' ', $str );
  192. $str = strip_tags( $str );
  193. $str = preg_replace( '/ss+/', ' ', $str );
  194. $str = substr( $str, 0, $chars );
  195. $str = preg_replace( '/ss+/', ' ', $str );
  196. $arr = explode( ' ', $str );
  197. array_pop( $arr );
  198. //$arr = preg_replace('/&nbsp;/i',' ',$arr);
  199. $final = implode( ' ', $arr ) . $info;
  200. }
  201. else
  202. {
  203. $final = $str;
  204. }
  205. return $final;
  206. }
  207.  
  208. }
  209.  
  210. CREATE TABLE minhatabela (
  211. id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  212. titulo varchar(300) DEFAULT NULL
  213. ) ENGINE=InnoDB CHARACTER SET=utf8 COLLATE utf8_unicode_ci;
  214.  
  215. $conn = new PDO('mysql:host=HOST;dbname=BANCO;charset=utf-8', 'USUARIO', 'SENHA');
  216. $conn->exec('SET CHARACTER SET utf8');//Define o charset como UTF-8
  217.  
  218. $mysqli = new mysqli('HOST', 'usuario', 'senha', 'banco');
  219.  
  220. if ($mysqli->connect_error) {
  221. printf('Erro de conexão: %s', $mysqli->connect_errno);
  222. exit;
  223. }
  224.  
  225. /*
  226. * compatibilidade para to 5.2.9 and 5.3.0.
  227. */
  228. if (mysqli_connect_error()) {
  229. printf('Erro de conexão: %s', mysqli_connect_error());
  230. exit;
  231. }
  232.  
  233. if (false === $mysqli->set_charset('utf8')) {
  234. printf('Error ao usar utf8: %s', $mysqli->error);
  235. exit;
  236. }
  237.  
  238. <?php
  239. $link = mysqli_connect('HOST', 'usuario', 'senha', 'banco');
  240.  
  241. if (mysqli_connect_error()) {
  242. printf('Erro de conexão: %s', mysqli_connect_error());
  243. exit;
  244. }
  245.  
  246. if (!mysqli_set_charset($link, 'utf8')) {
  247. printf('Error ao usar utf8: %s', mysqli_error($link));
  248. exit;
  249. }
  250.  
  251. <?php
  252. header('Content-Type: text/html; charset=UTF-8');
  253.  
  254. echo 'Conteudo';
  255.  
  256. <meta charset="UTF-8">
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement