class SqlHelper
{
public $conn;
public $host="localhost";
public $username="root";
public $password="root";
public $dbname="mydb";
public function __construct(){
$this->conn=mysql_connect($this->host,$this->username,$this->password);
if(!$this->conn)
{
die("链接失败".mysql_error());
}
mysql_select_db($this->dbname,$this->conn);
mysql_query("set names utf8");
}
//返回单条记录
public function execute_dql_one($sql)
{
$res=mysql_query($sql,$this->conn) or die("execute_dql".mysql_error());
$row=mysql_fetch_row($res);
$one=$row[0];
mysql_free_result($res);
return $one;
}
//返回单行记录
public function execute_dql_single_line($sql)
{
$res=mysql_query($sql,$this->conn) or die("execute_dql".mysql_error());
return mysql_fetch_assoc($res);
}
//返回多行记录
public function execute_dql_multi_line($sql)
{
$arr=array();
$res=mysql_query($sql,$this->conn) or die(mysql_error());
while (!!$row=mysql_fetch_assoc($res))
{
$arr[]=$row;
}
mysql_free_result($res);
return $arr;
}
public function execute_dml($sql)
{
$b=mysql_query($sql,$this->conn) or die(mysql_error());
if(!$b){
return 0;
}else{
if(mysql_affected_rows($this->conn)>0){
return 1;//表示执行OK
}else{
return 2;//表示没有行受到影响
}
}
}
//sql1:select * from 表名 limit 0,6
//sql2:select count(id) from 表名
public function execute_dql_fenye($tableName,$fenyePage)
{
$sql1="select count(*) from ".$tableName;
$res=mysql_query($sql1,$this->conn) or die(mysql_error());
$row=mysql_fetch_row($res);
$fenyePage->rowCount=$row[0];
mysql_free_result($res);
$fenyePage->pageCount=ceil($fenyePage->rowCount/$fenyePage->pageSize);
if(!empty($fenyePage->pageNow))
{
if (is_numeric($fenyePage->pageNow))
{
if($fenyePage->pageNow<1)
{
$fenyePage->pageNow=1;
}
if($fenyePage->pageNow>$fenyePage->pageCount)
{
$fenyePage->pageNow=$fenyePage->pageCount;
}
}else{
$fenyePage->pageNow=1;
}
}else{
$fenyePage->pageNow=1;
}
$sql2="select * from ".$tableName." order by id limit ".$fenyePage->pageSize*($fenyePage->pageNow-1).",".$fenyePage->pageSize;
$res=mysql_query($sql2,$this->conn) or die(mysql_error());
$arr=array();
while (!!$row=mysql_fetch_assoc($res))
{
$arr[]=$row;
}
$fenyePage->res_array=$arr;
$fenyePage->navigate.="当前页码$fenyePage->pageNow/$fenyePage->pageCount,总共有{$fenyePage->rowCount}条,每页显示{$fenyePage->pageSize}条
";
if($fenyePage->pageNow>1){
$prePage=$fenyePage->pageNow-1;
$fenyePage->navigate.="上一页";
}
if($fenyePage->pageNow<$fenyepage->pageCount){
$nextPage=$fenyePage->pageNow+1;
$fenyePage->navigate.="下一页";
}
$start=floor(($fenyePage->pageNow-1)/$fenyePage->page_whole)*$fenyePage->page_whole+1;
$Index=$start;
//向前整体翻页
/*if ($start>$fenyePage->page_whole){
$fenyePage->pageNow_pre=$start-$fenyePage->page_whole;
echo " << ";
}*/
if($fenyePage->pageNow>$fenyePage->page_whole)
{
$fenyePage->navigate.=" << ";
}
//整体翻页
for($start=$Index;$start<$index+$fenyepage->page_whole;$start++)
{
if($start<=$fenyepage->pageCount)
{
$fenyePage->navigate.="$start ";
}
}
//向后整体翻页
if($start<=$fenyepage->pageCount)
{
$fenyePage->navigate.=" >>
";
}
mysql_free_result($res);
//mysql_close($this->conn);
}
public function close_connect(){
if(!empty($this->conn)){
mysql_close($this->conn);
}
}
}