- <?php
- /*
- 作者:夜无眠
- QQ:27262681
- 用法简单示例:
- $data->maxpage = 0; //分页每页显示记录条数
- $data->fields = "adminame"; //需要显示的记录数
- $data->table = TBL_MAILSUBSCRIBE; //查询的数据表
- unset($what);
- $what[] = " and `title` like '%a%' ";//查询的条件
- $what['name'] = 'aa'; //查询的条件
- $what['id'] = array(1,3,4,5,6);//查询条件
- $order = "order by `time` desc"; //排序
- $guides = $data->get($what);
- */
- class date{
- public $fields = "*"; //字段
- public $maxpage = 10; //分页
- public $page_onoff = 1; //是否打开分页
- public $table = ""; //表名
- public $count = 0; //是否仅统计记录数
- function get($what = null,$order = ""){
- global $db;//数据库类实例
- if (is_array($what)){
- foreach ($what as $key => $val){
- if (is_int($key)){
- $where .= " $val ";
- }elseif(is_array($val)){
- $where .= " and `{$key}` in ('".implode("','",$val)."') ";
- }elseif($val === "" or $val === null){
- $where .= " and (`{$key}` is null or `{$key}` ='') ";
- }else{
- $where .= (strval(floatval($val))==$val) ? " and `{$key}`={$val} " : " and `{$key}`='{$val}' ";
- }
- }
- }
- $this->sql = $sql = "select {$this->fields} from `".$this->table."` where 1 {$where} {$order}";
- if ( $this->maxpage != 0 ){ //
- if ($this->page_onoff == 0){
- $num = $this->maxpage;
- }else{
- $sqlc = "select count(*) as `num` from `".$this->table."` where 1 {$where}";
- $query = $db->query($sqlc);
- $rs = $db->fetch_assoc($query);
- $num = $rs["num"];
- if ($this->count == 1){
- return $num;
- }
- }
- $page = new page($num,$this->maxpage);
- $page->showtype = 2;
- $this->sql = $sql .= $page->limit();
- $this->pageshow = $page->printPage();
- $this->pageinfo = $page->PrintPageinfo();
- }
- $query = $db->query($sql);
- while ($rs = $db->fetch_assoc($query)){
- $re[] = $rs;
- }
- return $re;
- }
- }
- ?>
上面的数据库单表查询类还需要下面的数据库类和分页类配合
- <?php
- /*
- mysql数据连接类文件
- */
- class DB{
- function connect($host,$user,$pwd,$dbname){
- if ( !($this->link= @mysql_connect($host,$user,$pwd)) )
- {
- $this->link = @mysql_connect('jp.made-in-china.co.jp',$user,$pwd);
- }
- @mysql_select_db($dbname,$this->link) or $this->halt("数据库发生了点小错误!一会就好".mysql_error());
- if($this->version() > '4.1') {
- global $config;
- if(!$config['dbcharset'] && in_array(strtolower($config['charset']), array('gbk', 'big5', 'utf-8'))) {
- $config['dbcharset'] = str_replace('-', '', $config['charset']);
- }
- if($config['dbcharset']) {
- mysql_query("SET character_set_connection='".$config['dbcharset']."', character_set_results='".$config['dbcharset']."', character_set_client=binary");
- }
- if($this->version() > '5.0.1') {
- mysql_query("SET sql_mode=''");
- }
- }
- }
- function __destruct(){
- //$this->close();
- }
- function delete($tbl,$where,$limit=0,$debug=0)
- {
- $openlimit = $limit ? " LIMIT $limit" : "";
- $sql = "DELETE FROM ".$tbl." WHERE " .$where . $openlimit;
- if ($debug) echo $sql;
- $this->query($sql);
- unset($sql,$openlimit);
- return $this->affected_rows();
- }
- function insert($table,$arrdata, $writelog=false , $debug=false)
- {
- foreach($arrdata as $keys=>$vals){ //取侄渭值址
- $keystr .= $keystr ? ',`'.$keys.'`' : '`'.$keys.'`'; //侄址
- $valstr .= $valstr ? ",'".$vals."'" : "'".$vals."'"; //值址
- }
- //菘
- $sql = "INSERT INTO $table (".$keystr.") VALUES (".$valstr.")";
- $this->query($sql); //
- if ($debug===true)
- {
- echo "<pre>".$sql."</pre><br/>";
- }
- $uid = $this->insert_id(); //取UID
- unset($arrdata,$sql);
- if(!$uid){
- return false;
- }else{
- return $uid;
- }
- }
- /* :嗉?
- 荼:$table:; $arrdata:侄榧爸? $wherestr:址
- 时:XDF.2008-1-9 时:XDF.2008-1-9
- */
- function update($table,$array,$where, $debug=false)
- {
- //菘
- $data = NULL;
- if ( is_array($array) )
- {
- foreach($array as $key=>$val)
- {
- $data.= $data ? ",`$key`='$val'" : "`$key`='$val'";
- }
- }else{
- echo "值式";
- }
- $sql = "UPDATE $table SET $data WHERE $where";
- $this->query($sql); //
- if ($debug)
- {
- echo $sql;
- }
- unset($array,$data,$sql,$where);
- if($this->affected_rows()){
- return true;
- }else{
- return false;
- }
- }
- //2008年2月22日,水月添加,一次插入多行记录
- //$arrdata 结构:
- //$arrdata[0][’字段名'] = 第一行数据;
- //$arrdata[1][’字段名'] = 第二行数据;
- //注意字段要补齐,索引要按照自然数排列,否则会造成数据错误;
- function insert_mult($table,$arrdata)
- {
- $set_buffer = 300;//设置缓冲行数
- $keys = array_keys((array)$arrdata[0]);
- $fields = '(`'.implode('`,`',$keys).'`)';
- $sql = NULL;
- $d = 'INSERT INTO `'.$table.'` '.$fields.' VALUES ';
- foreach((array)$arrdata as $key=>$value)
- {
- $sql .= $d.'(/''.implode('/',/'',$value).'/')';
- $d = ',';
- if(($key+1) % $set_buffer == 0)
- {
- $this->query($sql);
- $d = 'INSERT INTO `'.$table.'` '.$fields.' VALUES ';
- $sql = NULL;
- }
- }
- if(!is_null($sql))
- {
- $this->query($sql);
- }
- unset($sql,$fields,$table);
- return true;
- }
- function query($sql,$debug=''){
- global $executes;
- $this->sql = $sql;
- $type = '';
- $this->sqltxt .= $sql."/n";
- if (emptyempty($sql)){
- echo get_parent_class();
- }
- $unixtime = microtime_float();
- $query = mysql_query($sql,$this->link) or die('SQL'.$sql.'<br />'.mysql_Error());
- if (emptyempty($debug) === false)
- {
- echo "<per>".$sql."</pre>";
- }
- $endunixtime = round(microtime_float() - $unixtime ,4);
- //if ($endunixtime >=0.4){
- //print("<pre>$sql</pre>");
- //wfile(ROOT.'data/log/sql.txt', $sql."==".$endunixtime."/n/t","a");
- //}
- $executes++;
- return $query;
- }
- function free_result($result){ //??
- return mysql_free_result($result);
- }
- function close(){
- mysql_close($this->link); //???
- }
- function affected_rows() {
- return mysql_affected_rows($this->link); //???
- }
- function error() {
- return mysql_error();//??
- }
- function errno() {
- return intval(mysql_errno()); //?
- }
- function insert_id() {
- return mysql_insert_id($this->link);
- }
- function fetch_row($query) {
- return mysql_fetch_row($query);
- }
- function fetch_fields($query) {
- return mysql_fetch_field($query);
- }
- function version() {
- return mysql_get_server_info($this->link);
- }
- function num_rows($query) {
- return mysql_num_rows($query);
- }
- function num_fields($result) {
- return mysql_num_fields($result);
- }
- function fetch_array($query) {
- return mysql_fetch_array($query);
- }
- function halt($message = '', $sql = '') {
- $fp = fopen(ROOT.'data/dberror.log', 'a');
- fwrite($fp, 'SQL:'.$sql.'File:'.$_SERVER['REQUEST_URI'].'/n/r/t');
- fclose($fp);
- }
- function list_tables($dbname){
- return mysql_list_tables($dbname);
- }
- function fetch_assoc($result)
- {
- return mysql_fetch_assoc($result);
- }
- }
- if(!function_exists("file_get_contents")){
- function file_get_contents($path){
- return implode('',@file($path));
- }
- }
- function sqlsafe($sql){
- if (strpos($sql,"/'")!==false or strpos($sql,'/"')!==false){
- return $sql;
- }
- $sql=str_replace("//","",$sql);
- $sql=str_replace("'","/'",$sql);
- $sql=trim($sql);
- return $sql;
- }
- ?>
- <?php
- /**********************************************************************
- 请保留版权信息
- 系统版本shwomov4.2
- 作者MSN:jdzcn_net@hotmail.com
- 开发商:天天网络专业视频点播CMS系统开发商
- http://www.showmov.com演示站
- http://www.jdzcn.net官方站
- *********************************************************************/
- class PAGE{
- var $page =1;
- var $nextpage =9;
- var $total;
- var $p;
- var $showtype=1,$pageinfo,$totalpage;
- var $MaxPage = 10; //定义分页数
- var $lang;
- function __construct($total,$maxpage=10){
- //生成分页内容
- $totalpage = ceil($total / $maxpage);
- if(! $totalpage){
- $totalpage=1;
- }
- $page = intval($_GET['page']);
- if(! $page ){
- $page = 1;
- }elseif($page>$totalpage){
- $page = $totalpage;
- }
- $this->page=$page;
- $this->MaxPage = $maxpage;
- $this->total=$total;
- $this->totalpage=$totalpage;
- $this->lang = $GLOBALS['lang'];
- $QueryString="";
- $array = array_merge($_POST,$_GET);
- foreach($array as $key=>$name){
- if($key!='Sunmit' && $key!="page"){
- /*
- 夜无眠6月5日修改
- */
- if (is_array($name)){
- foreach ($name as $k => $v){
- $QueryString.=$QueryString ? "&{$key}[{$k}]=$v":"?{$key}[{$k}]=$v";
- }
- }else{
- if($key=='keyword'){
- $name=urlencode($name);
- }
- $QueryString.=$QueryString ? "&$key=$name":"?$key=$name";
- }
- /*
- */
- }
- }
- //==
- if($totalpage > $this->nextpage){
- $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>";
- }
- if ($page<$this->nextpage)
- {
- $start = 1;
- }else{
- $start = floor($page / $this->nextpage)*$this->nextpage;
- }
- $cutpage = $start + $this->nextpage;
- if ($cutpage > $totalpage)
- {
- $cutpage = $totalpage;
- }
- for($i= $start;$i<=$cutpage;$i++){//循环列出
- if($page==$i){
- $p.="<a class=/"p_curpage/">$i</a>";
- }
- else{
- $p.="<a class=/"p_num/" href=/"$QueryString&page=$i/">$i</a>";
- }
- }
- if($page<$totalpage){
- $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>";
- }
- $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>";
- $p.="</div>";
- unset($cutpage,$totalpage,$page,$start,$i);
- $this->p = $p;
- }
- public function PrintPage(){
- //打印分页
- $s = "<div class=/"p_bar/">";
- switch($this->showtype){//判断显示模式
- case 1:
- $s .= "<a class=/"p_total/"> ".$this->total." | ".$this->lang->maxpage." </a><a class=/"p_pages/"> $this->page/$this->totalpage </a>";
- break;
- case 2:
- $temp=$this->MaxPage*$this->page;
- $starts=$this->MaxPage*($this->page-1)+1;
- $ends=$temp>$this->total ? $this->total : $temp;
- $this->pageinfo = $starts."-".$ends."".$this->lang->dangwei_tiao." ".$this->lang->total.":".$this->total.$this->lang->dangwei_tiao." $this->page/$this->totalpage ".$thia->lang->page." ";
- break;
- }
- return $s.$this->p;
- }
- public function limit(){
- //输出sql--limit
- return " LIMIT ".($this->page-1)*$this->MaxPage.",".$this->MaxPage;
- }
- public function PrintPageinfo(){
- //打印分页信息
- return $this->pageinfo;
- }
- }
- ?>