数据库单表查询类

 
  1. <?php
  2. /*
  3.     作者:夜无眠
  4.     QQ:27262681
  5.     用法简单示例:
  6.     $data->maxpage = 0; //分页每页显示记录条数
  7.     $data->fields = "adminame"; //需要显示的记录数
  8.     $data->table = TBL_MAILSUBSCRIBE; //查询的数据表
  9.     unset($what);
  10.     $what[] = " and `title` like '%a%' ";//查询的条件
  11.     $what['name'] = 'aa'; //查询的条件
  12.     $what['id'] = array(1,3,4,5,6);//查询条件
  13.     $order = "order by `time` desc"; //排序
  14.     $guides = $data->get($what);
  15. */
  16. class date{
  17.     public $fields = "*"//字段
  18.     public $maxpage = 10; //分页
  19.     public $page_onoff = 1; //是否打开分页
  20.     public $table = "";  //表名
  21.     public $count = 0; //是否仅统计记录数
  22.     function get($what = null,$order = ""){
  23.         global $db;//数据库类实例
  24.         if (is_array($what)){
  25.             foreach ($what as $key => $val){
  26.                 if (is_int($key)){
  27.                     $where .= " $val ";
  28.                 }elseif(is_array($val)){
  29.                     $where .= " and `{$key}` in ('".implode("','",$val)."') ";                  
  30.                 }elseif($val === "" or $val === null){
  31.                     $where .= " and (`{$key}` is null or `{$key}` ='') ";
  32.                 }else{
  33.                     $where .= (strval(floatval($val))==$val) ? " and `{$key}`={$val} " : " and `{$key}`='{$val}' ";
  34.                 }
  35.             }
  36.         }
  37.         $this->sql = $sql = "select {$this->fields} from `".$this->table."`  where 1 {$where} {$order}";
  38.         if ( $this->maxpage != 0 ){ //
  39.             if ($this->page_onoff == 0){
  40.                 $num = $this->maxpage;
  41.             }else{
  42.                 $sqlc = "select count(*) as `num` from `".$this->table."`  where 1 {$where}";
  43.                 $query = $db->query($sqlc);
  44.                 $rs = $db->fetch_assoc($query);
  45.                 $num = $rs["num"];
  46.                 if ($this->count == 1){
  47.                     return $num;
  48.                 }
  49.             }
  50.             $page = new page($num,$this->maxpage);
  51.             $page->showtype = 2;
  52.             $this->sql = $sql .= $page->limit();
  53.             $this->pageshow = $page->printPage();
  54.             $this->pageinfo = $page->PrintPageinfo();
  55.         }
  56.         $query = $db->query($sql);
  57.         while ($rs = $db->fetch_assoc($query)){
  58.             $re[] = $rs;
  59.         }
  60.         return $re;
  61.     }
  62. }
  63. ?>

 

上面的数据库单表查询类还需要下面的数据库类和分页类配合

 

  1. <?php
  2. /*
  3. mysql数据连接类文件
  4. */
  5. class DB{
  6.     function connect($host,$user,$pwd,$dbname){
  7.         if ( !($this->link= @mysql_connect($host,$user,$pwd)) )
  8.         {
  9.             $this->link = @mysql_connect('jp.made-in-china.co.jp',$user,$pwd);
  10.         }
  11.         @mysql_select_db($dbname,$this->link) or $this->halt("数据库发生了点小错误!一会就好".mysql_error());
  12.         if($this->version() > '4.1') {
  13.             global $config;
  14.             if(!$config['dbcharset'] && in_array(strtolower($config['charset']), array('gbk''big5''utf-8'))) {
  15.                 $config['dbcharset'] = str_replace('-'''$config['charset']);
  16.             }
  17.             if($config['dbcharset']) {
  18.                 mysql_query("SET character_set_connection='".$config['dbcharset']."', character_set_results='".$config['dbcharset']."', character_set_client=binary");
  19.             }
  20.             if($this->version() > '5.0.1') {
  21.                 mysql_query("SET sql_mode=''");
  22.             }
  23.         }
  24.     }
  25.     function __destruct(){
  26.         //$this->close();
  27.     }
  28.     function delete($tbl,$where,$limit=0,$debug=0)
  29.     {
  30.         $openlimit = $limit ? " LIMIT $limit" : "";
  31.         $sql = "DELETE FROM ".$tbl." WHERE " .$where . $openlimit;
  32.         if ($debugecho $sql;
  33.         $this->query($sql);
  34.         unset($sql,$openlimit);
  35.         return $this->affected_rows();
  36.     }
  37.     function insert($table,$arrdata$writelog=false , $debug=false)
  38.     {
  39.         foreach($arrdata as $keys=>$vals){  //取侄渭值址
  40.             $keystr .= $keystr ? ',`'.$keys.'`' : '`'.$keys.'`';    //侄址
  41.             $valstr .= $valstr ? ",'".$vals."'" : "'".$vals."'";    //值址
  42.         }
  43.         //菘
  44.         $sql    = "INSERT INTO $table (".$keystr.") VALUES (".$valstr.")";
  45.         $this->query($sql); //
  46.         if ($debug===true)
  47.         {
  48.             echo "<pre>".$sql."</pre><br/>";
  49.         }
  50.         $uid    = $this->insert_id();   //取UID
  51.         unset($arrdata,$sql);
  52.         if(!$uid){
  53.             return false;
  54.         }else{
  55.             return $uid;
  56.         }
  57.     }
  58.     /*  :嗉?
  59.         荼:$table:; $arrdata:侄榧爸? $wherestr:址
  60.         时:XDF.2008-1-9 时:XDF.2008-1-9
  61.     */
  62.     function update($table,$array,$where$debug=false)
  63.     {
  64.         //菘
  65.         $data = NULL;
  66.         if ( is_array($array) )
  67.         {
  68.             foreach($array as $key=>$val)
  69.             {
  70.                 $data.= $data ? ",`$key`='$val'" : "`$key`='$val'";
  71.             }
  72.         }else{
  73.             echo "值式";
  74.         }
  75.         $sql = "UPDATE $table SET $data WHERE $where";
  76.         $this->query($sql); //
  77.         if ($debug)
  78.         {
  79.             echo $sql;
  80.         }
  81.         unset($array,$data,$sql,$where);
  82.         if($this->affected_rows()){
  83.             return true;
  84.         }else{
  85.             return false;
  86.         }
  87.     }
  88.     
  89.     //2008年2月22日,水月添加,一次插入多行记录
  90.     //$arrdata 结构:
  91.     //$arrdata[0][’字段名'] = 第一行数据;
  92.     //$arrdata[1][’字段名'] = 第二行数据;
  93.     //注意字段要补齐,索引要按照自然数排列,否则会造成数据错误;
  94.     function insert_mult($table,$arrdata)
  95.     {
  96.         $set_buffer = 300;//设置缓冲行数
  97.         $keys = array_keys((array)$arrdata[0]);
  98.         $fields = '(`'.implode('`,`',$keys).'`)';
  99.         
  100.         $sql = NULL;
  101.         $d = 'INSERT INTO `'.$table.'` '.$fields.' VALUES ';
  102.         foreach((array)$arrdata as $key=>$value)
  103.         {
  104.             $sql .= $d.'(/''.implode('/',/'',$value).'/')';
  105.             $d = ',';
  106.             if(($key+1) % $set_buffer == 0)
  107.             {
  108.                 $this->query($sql);
  109.                 $d = 'INSERT INTO `'.$table.'` '.$fields.' VALUES ';
  110.                 $sql = NULL;
  111.             }
  112.         }
  113.         if(!is_null($sql))
  114.         {
  115.             $this->query($sql);
  116.         }
  117.         
  118.         unset($sql,$fields,$table);
  119.         return true;
  120.     }
  121.     
  122.     
  123.     function query($sql,$debug=''){
  124.         global $executes;
  125.         $this->sql = $sql;
  126.         $type = '';
  127.         $this->sqltxt .= $sql."/n";
  128.         if (emptyempty($sql)){
  129.             echo get_parent_class();
  130.         }
  131.         $unixtime = microtime_float();
  132.         $query = mysql_query($sql,$this->link) or die('SQL'.$sql.'<br />'.mysql_Error());
  133.         if (emptyempty($debug) === false)
  134.         {
  135.             echo "<per>".$sql."</pre>";
  136.         }
  137.         $endunixtime = round(microtime_float() - $unixtime ,4);
  138.         //if ($endunixtime >=0.4){
  139.             //print("<pre>$sql</pre>");
  140.             //wfile(ROOT.'data/log/sql.txt', $sql."==".$endunixtime."/n/t","a");
  141.         //}
  142.         $executes++;
  143.         return $query;
  144.     }
  145.     function free_result($result){  //??
  146.         return mysql_free_result($result);
  147.     }
  148.     function close(){
  149.         mysql_close($this->link);   //???
  150.     }
  151.     function affected_rows() {
  152.         return mysql_affected_rows($this->link);    //???
  153.     }
  154.     function error() {
  155.         return mysql_error();//??
  156.     }
  157.     function errno() {
  158.         return intval(mysql_errno());   //?
  159.     }
  160.     function insert_id() {
  161.         return mysql_insert_id($this->link);
  162.     }
  163.     function fetch_row($query) {
  164.         return mysql_fetch_row($query);
  165.     }
  166.     function fetch_fields($query) {
  167.         return mysql_fetch_field($query);
  168.     }
  169.     function version() {
  170.         return mysql_get_server_info($this->link);
  171.     }
  172.     function num_rows($query) {
  173.         return mysql_num_rows($query);
  174.     }
  175.     function num_fields($result) {
  176.         return mysql_num_fields($result);
  177.     }
  178.     function fetch_array($query) {
  179.         return mysql_fetch_array($query);
  180.     }
  181.     function halt($message = ''$sql = '') {
  182.             $fp = fopen(ROOT.'data/dberror.log''a');
  183.             fwrite($fp'SQL:'.$sql.'File:'.$_SERVER['REQUEST_URI'].'/n/r/t');
  184.             fclose($fp);
  185.     }
  186.     function list_tables($dbname){
  187.         return mysql_list_tables($dbname);
  188.     }
  189.     function fetch_assoc($result)
  190.     {
  191.         return mysql_fetch_assoc($result);
  192.     }
  193.     
  194. }
  195. if(!function_exists("file_get_contents")){
  196.     function file_get_contents($path){
  197.         return implode('',@file($path));
  198.     }
  199. }
  200. function sqlsafe($sql){
  201.     if (strpos($sql,"/'")!==false or strpos($sql,'/"')!==false){
  202.         return $sql;
  203.     }
  204.     $sql=str_replace("//","",$sql);
  205.     $sql=str_replace("'","/'",$sql);
  206.     $sql=trim($sql);
  207.     return $sql;
  208. }
  209. ?>

 

  1. <?php
  2. /**********************************************************************
  3. 请保留版权信息
  4. 系统版本shwomov4.2
  5. 作者MSN:jdzcn_net@hotmail.com
  6. 开发商:天天网络专业视频点播CMS系统开发商
  7. http://www.showmov.com演示站
  8. http://www.jdzcn.net官方站
  9. *********************************************************************/
  10. class PAGE{
  11.     var $page =1;
  12.     var $nextpage =9;
  13.     var $total;
  14.     var $p;
  15.     var $showtype=1,$pageinfo,$totalpage;
  16.     var $MaxPage = 10;          //定义分页数
  17.     var $lang;
  18.     function __construct($total,$maxpage=10){
  19.         //生成分页内容
  20.         $totalpage  = ceil($total / $maxpage);
  21.         if(! $totalpage){
  22.             $totalpage=1;
  23.         }
  24.         $page       = intval($_GET['page']);
  25.         if(! $page ){
  26.             $page   = 1;
  27.         }elseif($page>$totalpage){
  28.             $page = $totalpage;
  29.         }
  30.         $this->page=$page;
  31.         $this->MaxPage = $maxpage;
  32.         $this->total=$total;
  33.         $this->totalpage=$totalpage;
  34.         $this->lang = $GLOBALS['lang'];
  35.         $QueryString="";
  36.         $array = array_merge($_POST,$_GET);
  37.         foreach($array as $key=>$name){
  38.             if($key!='Sunmit' && $key!="page"){
  39.                 /*
  40.                 夜无眠6月5日修改
  41.                 */
  42.                 if (is_array($name)){
  43.                     foreach ($name as $k => $v){
  44.                         $QueryString.=$QueryString ? "&{$key}[{$k}]=$v":"?{$key}[{$k}]=$v";
  45.                     }
  46.                 }else{
  47.                     if($key=='keyword'){
  48.                         $name=urlencode($name);
  49.                     }
  50.                     $QueryString.=$QueryString ? "&$key=$name":"?$key=$name";
  51.                 }
  52.                 /*
  53.                 */
  54.             }
  55.         }
  56.         //==
  57.         if($totalpage > $this->nextpage){
  58.             $p.="<a class=/"p_redirect/" href=/"$QueryString&page=1/" title=/"".$this->lang->index."/">|<span class=/"jiantou/"><</span></a><a class=/"p_redirect/" href=/"$QueryString&page=".($page-1)."/" title=/"上一页/"><span class=/"jiantou  p_frame/"><<</span></a>";
  59.         }
  60.         if ($page<$this->nextpage)
  61.         {
  62.             $start = 1;
  63.         }else{
  64.             $start = floor($page / $this->nextpage)*$this->nextpage;
  65.         }
  66.         $cutpage = $start + $this->nextpage;
  67.         if ($cutpage > $totalpage)
  68.         {
  69.             $cutpage = $totalpage;
  70.         }
  71.         for($i$start;$i<=$cutpage;$i++){//循环列出
  72.                 if($page==$i){
  73.                     $p.="<a class=/"p_curpage/">$i</a>";
  74.                 }
  75.                 else{
  76.                     $p.="<a class=/"p_num/" href=/"$QueryString&page=$i/">$i</a>";
  77.                 }
  78.         }
  79.         if($page<$totalpage){
  80.             $p.="<a class=/"p_redirect/" href=/"$QueryString&page=".($page+1)."/" title=/"".$this->lang->netpage."/"><span class=/"jiantou p_frame/">>></span></a><a class=/"p_redirect/" href=/"$QueryString&page=$totalpage/" title=/"".$this->lang->endpage."/"><span class=/"jiantou/">></span>|</a>";
  81.         }
  82.         $p.="<a class=/"p_pages/" style=/"padding: 0px/"><input class=/"p_input/" type=/"text/" id=custompage name=/"custompage/" onKeyDown=/"if(event.keyCode==13) {window.location='$QueryString&page='+this.value; return false;}/" title='".$this->lang->gotopage."'></a><a class=/"p_redirect/" href=/"javascript:location='$QueryString&page='+document.getElementById('custompage').value/">GO</a>";
  83.         $p.="</div>";
  84.         unset($cutpage,$totalpage,$page,$start,$i);
  85.         $this->p = $p;
  86.         
  87.     }
  88.     public function PrintPage(){
  89.         //打印分页
  90.         $s = "<div class=/"p_bar/">";
  91.         switch($this->showtype){//判断显示模式
  92.             case 1:
  93.                 $s .= "<a class=/"p_total/"> ".$this->total." | ".$this->lang->maxpage." </a><a class=/"p_pages/"> $this->page/$this->totalpage </a>";
  94.                 break;
  95.             case 2:
  96.                 $temp=$this->MaxPage*$this->page;
  97.                 $starts=$this->MaxPage*($this->page-1)+1;
  98.                 $ends=$temp>$this->total ? $this->total : $temp;
  99.                 $this->pageinfo = $starts."-".$ends."".$this->lang->dangwei_tiao."  ".$this->lang->total.":".$this->total.$this->lang->dangwei_tiao."  $this->page/$this->totalpage ".$thia->lang->page." ";
  100.                 break;
  101.         }
  102.         return $s.$this->p;
  103.     }
  104.     
  105.     public function limit(){
  106.         //输出sql--limit
  107.         return " LIMIT ".($this->page-1)*$this->MaxPage.",".$this->MaxPage;
  108.     }
  109.     public function PrintPageinfo(){
  110.         //打印分页信息
  111.         return $this->pageinfo;
  112.     }
  113. }
  114. ?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值