相關(guān)關(guān)鍵詞
關(guān)于我們
最新文章
php中分頁及SqlHelper類用法實例

本文實例講述了php中分頁及SqlHelper類用法。分享給大家供大家參考,具體如下:
文檔目錄結(jié)構(gòu)如下:
SqlHelper.php代碼如下:
<?php /** * Created by JetBrains PhpStorm. * User: lee * Date: 13-7-26 * Time: 下午8:30 * To change this template use File | Settings | File Templates. */ class SqlHelper{ private $mysqli; private static $host="localhost"; private static $user="root"; private static $pwd=""; private static $db="world"; private $sql=false; private $result=false; function __construct(){ $this->mysqli=new MySQLi(self::$host,self::$user,self::$pwd,self::$db); if($this->mysqli->connect_error){ die("連接數(shù)據(jù)庫失??! ".$this->mysql->connect_error); } $this->mysqli->query("set names utf8"); } function execute_dql_all($sql){ //執(zhí)行查詢語句 $arr=array(); $this->result=$this->mysqli->query($sql) or die ($this->mysql->connect_error); //將數(shù)據(jù)轉(zhuǎn)存到$arr數(shù)組中 while($row=mysqli_fetch_array($this->result,MYSQL_BOTH)){ $arr[]=$row; } $this->result->free(); return $arr; } function execute_dql_num($sql){ //執(zhí)行查詢語句 $arr=array(); $this->result=$this->mysqli->query($sql) or die ($this->mysql->connect_error); //將數(shù)據(jù)轉(zhuǎn)存到$arr數(shù)組中 while($row=mysqli_fetch_array($this->result,MYSQLI_NUM)){ $arr[]=$row; } $this->result->free(); return $arr; } function execute_dql_assoc($sql){ //執(zhí)行查詢語句 $arr=array(); $this->result=$this->mysqli->query($sql) or die ($this->mysql->connect_error); //將數(shù)據(jù)轉(zhuǎn)存到$arr數(shù)組中 while($row=mysqli_fetch_array($this->result,MYSQLI_ASSOC)){ $arr[]=$row; } $this->result->free(); return $arr; } //查詢某表中的記錄數(shù) function execute_dql_counts($table,$id="*"){ $this->sql="select count($id) from $table"; $this->result=$this->mysqli->query($this->sql); $row=mysqli_fetch_all($this->result); $this->result->free(); return $row[0][0]; } function execute_dml($sql){ //執(zhí)行正刪改 $this->result=$this->mysqli->query($sql); if(!$this->result){ return -1;//執(zhí)行正刪改失敗 }else{ if($this->mysqli->affected_rows>0){ return 1;//執(zhí)行正刪改成功,影響行數(shù) }else{ return 0;//執(zhí)行正刪改成功,但沒有影響行數(shù) } } } }