Ver código fonte

恢复代码

chenbo 5 anos atrás
pai
commit
1c99831623
3 arquivos alterados com 128 adições e 49 exclusões
  1. 3 1
      data/test.php
  2. 19 48
      lib/common/mypdo.class.php
  3. 106 0
      lib/table/_table_.class.php

+ 3 - 1
data/test.php

@@ -6,6 +6,8 @@
  * Time: 19:16
  */
 require('../init.php');
-var_dump($dbconn);
+
+global $mypdo;
+var_dump($mypdo);
 
 sina::get_day_k_base(601006);

+ 19 - 48
lib/common/mypdo.class.php

@@ -10,7 +10,7 @@
  * @createtime	2018/03/01
  * @author		空竹
  * @copyright	芝麻开发 (http://www.zhimawork.com)
- *
+ * 
  * 该类的使用方法简介:
  *
  * ------------SQL查询---------
@@ -20,7 +20,7 @@
  * $sql = "select * from shop where shop_name = $name ";
  * 执行查询:
  * $mypdo->sqlQuery($sql);
- *
+ * 
  * ------------SQL写入---------
  * 示例:
  * $param = array(
@@ -49,7 +49,7 @@
  */
 
 class MyPdo {
-
+    
     public  $pdo      = null;
 	public  $hostname = '';
 	public  $username = '';
@@ -58,28 +58,25 @@ class MyPdo {
     public  $debug    = false;
 	public  $log      = null;
 	public  $logSQL   = false;
-
+	
 	public function __construct(){
 		global $mylog;
 		$this->log = $mylog;
-//		$this->dbconnect();
 	}
 	public function dbconnect($hostname, $username, $password, $database, $prefix) {
 		$dsn = 'mysql:dbname='.$database.';host='.$hostname.';port=3306';
 		try {
-			$mypdo = new PDO($dsn, $username, $password);
+			$mypdo = new PDO($dsn, $username, $password); 
 			$mypdo -> query('set names utf8;');
 			$mypdo -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
-
 			$this->pdo = $mypdo;
 			$this->prefix = $prefix;
-//			var_dump($this);
 			return $this;
 		} catch(PDOException $e) {
 			if($this->debug) echo $e->getMessage();
 		}
 	}
-
+	
 	//数据库调试
 	public function debug($level = 0){
 		switch($level){
@@ -97,7 +94,7 @@ class MyPdo {
 				break;
 		}
 	}
-
+	
 	//查询
 	//本函数用于执行查询语句,增删改语句请使用其他专用函数
 	//@return 查询结果集array()。
@@ -108,7 +105,7 @@ class MyPdo {
 			//统计查询语句数量2016/6/16
 			$sqlactstr = strtolower(substr($sql, 0, 6));
 			if($sqlactstr == 'select') $CountSQLSelect++;
-
+			
 			$rs = $this -> pdo -> query($sql);
 			$i = 0;
 			$data = array();
@@ -117,7 +114,7 @@ class MyPdo {
 				$i++;
 			}
 			$rs->closeCursor();
-
+			
 			//2018/11/14记录SQL
 			if($this->logSQL) {
 				$this->log->debug($sql, 0);
@@ -134,57 +131,30 @@ class MyPdo {
 		}
 	}
 
-    /***
-     * @param $sql
-     * @return mixed
-     * 执行sql
-     */
-    public function execSql($sql)
-    {
-        try {
-           $mypdo = new MyPdo();//这里框架中的pdo不能用
-
-            $mypdo->exec($sql);//2018/11/14记录SQL
-            if($this->logSQL) {
-                $this->log->debug($sql, 0);
-            }
-            return $this->getLastId();
-        } catch(PDOException $e) {
-            $msg = $e->getMessage().'【错误的SQL语句:'.$sql.'】';
-            if($this->debug) {
-                echo $msg;
-            }else{
-                $this->log->debug($msg, 0);
-            }
-        }
-    }
-
 	//插入
-	//@param $param可选值
+	//@param $param可选值 
 	//             string--表示字符串;
-	//             number--表示数字
+	//             number--表示数字 
 	//             expression--表达式
 	//@return 成功执行返回刚插入的ID;
-
-
 	public function sqlinsert($table, $param){
 
 		if(!is_array($param)){
 			throw new Exception('sqlinsert参数错误', 905);
 		}
 		$sql = 'insert into '.$table.'(';
-
+		
 		$keys = array_keys($param);
 		$keys_str = implode(',', $keys);
 		$sql .= $keys_str.') values(';
-
+		
 		$params = array();
 		foreach($param as $val){
 			$params[] = $this->sql_check_input($val);
 		}
 		$params_str = implode(',', $params);
 		$sql .= $params_str.')';
-//var_dump($sql);
+
 		try {
 			$this->pdo->exec($sql);
 
@@ -192,6 +162,7 @@ class MyPdo {
 			if($this->logSQL) {
 				$this->log->debug($sql, 0);
 			}
+
 			return $this->getLastId();
 		} catch(PDOException $e) {
 			$msg = $e->getMessage().'【错误的SQL语句:'.$sql.'】';
@@ -220,7 +191,7 @@ class MyPdo {
 		$wheres_str = implode(' and ', $wheres);
 
 		$sql .= $wheres_str;
-
+		
 		try {
 			$num = $this->pdo->exec($sql);
 
@@ -264,7 +235,7 @@ class MyPdo {
 		$wheres_str = implode(' and ', $wheres);
 
 		$sql .= ' where '.$wheres_str;
-
+		
 
 		try {
 			$num = $this->pdo->exec($sql);
@@ -291,7 +262,7 @@ class MyPdo {
 	public function sql_check_input($arr_val)
 	{
 		$value = $arr_val[1];
-
+		
 		if (get_magic_quotes_gpc())
 		{
 			$value = stripslashes($value);
@@ -319,7 +290,7 @@ class MyPdo {
 
 	/**
 	 * sql_escape_mimic  用于在无mysql连接情况下替代mysql_real_escape_string的作用(防止SQL注入)
-	 *
+	 * 
 	 * @param mixed $str
 	 * @return
 	 */

+ 106 - 0
lib/table/_table_.class.php

@@ -0,0 +1,106 @@
+<?php
+
+/**
+ * 数据库表:(可将本文件另存为)
+ *
+ * @createtime	2018/10/17
+ * @author      空竹
+ * @copyright	芝麻开发(http://www.zhimawork.com)
+ */
+
+class Table_admin extends Table {
+
+	protected $table_name       = '';//表名,不带前缀
+	protected $table_id         = '';//指定ID字段名称,必须
+	protected $table_status     = '';//指定状态字段名称,如果有
+	protected $table_order      = '';//指定排序字段名称,如果有
+
+	//数据库结构
+	//完整展示数据库所有字段,替换字段名称
+	protected function struct(){
+		$attr = array();
+		
+		//$attr['id']      = 'xxx_id';
+
+		return $attr;
+	}
+
+	//增
+	//@param $attr array -- 键值同struct()返回的数组
+	public function add($attr){
+		
+		$param = array (
+			//'xxx'   => array('string', $attr['yyy'])
+		);
+        return $this->pdo->sqlinsert($this->table_fullname, $param);
+
+	}
+
+	//改
+	//@param $attr array -- 键值同struct()返回的数组
+	public function edit($id, $attr){
+		
+		$param = array (
+			//'xxx'   => array('string', $attr['yyy'])
+		);
+		$where = array (
+			//'xxx_id'   => array('number', $id)
+		);
+        return $this->pdo->sqlupdate($this->table_fullname, $param, $where);
+
+	}
+
+	//获取列表(分页)
+	//$count、$page和$pagesize都为0时,返回全部结果(适用于无需分页的情况)
+	//
+	//@param $filter array -- 过滤条件,格式见Table::filterToWhere
+	//@param $count -- 0:返回列表 1:返回结果数量
+	//@param $page -- 当前第几页
+	//@param $pagesize -- 每页数量
+	public function getList($filter = array(), $count = 0, $page = 0, $pagesize = 0){
+		
+		$where = $this->filterToWhere($filter);
+		
+		if($count == 0){//列表
+			
+			$sql = "select * from ". $this->table_fullname ." $where ";
+
+			if($this->table_order){//排序
+				$sql_order = " order by ". $this->table_order ." asc, ". $this->table_id ." desc ";
+			}else{
+				$sql_order = " order by ". $this->table_id ." desc ";
+			}
+			$sql .= $sql_order;
+
+			if($page > 0){//分页
+				$startrow = ($page - 1) * $pagesize;
+				$sql_limit = " limit $startrow, $pagesize";
+				$sql .= $sql_limit;
+			}
+			
+			$rs  = $this->pdo->sqlQuery($sql);
+			$r   = array();
+			if($rs){
+				foreach($rs as $key => $val){
+					$r[$key] = $this->dataToAttr($val);
+				}
+				return $r;
+			}else{
+				return $r;
+			}
+
+		}else{//统计
+			
+			$sql = "select count(*) as c from ". $this->table_fullname . " $where ";
+			$rs  = $this->pdo->sqlQuery($sql);
+			if($rs){
+				return $rs[0]['c'];
+			}else{
+				return 0;
+			}
+		}
+
+	}
+	
+}
+?>