php mysql数据库类

本文详细介绍了PHP中使用mysqli预处理语句的方法,包括参数绑定、结果集处理等,并提供了PDO类的实现方式,涵盖查询、插入、更新等数据库操作。

<?php

Mysqli

@header('Content-Type:text/html;charset=utf-8');
$mysqli = new mysqli("localhost","root","root","test");
/*//预处理  $insertid = $stmt->insert_id;新增id
$sql = "update user1 set names=?,pwd=? where id = ?";
$stmt = $mysqli->prepare($sql);
//绑定参数 i(integer) d(double) s(string) b(binary)
$stmt->bind_param("ssi",$names,$pwd,$id);
$id = 1;
$names = "cccc";
$pwd = "zzz";
$stmt->execute();
$id = 2;
$names = "dddddd";
$pwd = "zzz1";
$stmt->execute();*/

$sql = "select names,pwd from user1 where id > ?";

$stmt = $mysqli->prepare($sql);
$stmt->bind_param("i",$id);
$stmt->bind_result($names,$pwd);
$id = 0;
$stmt->execute();
/*$stmt->fetch();
echo $names,$pwd;
$stmt->fetch();
echo $names,$pwd;*/
$r = $stmt->store_result();//取出所有
echo "记录总数:".$stmt->num_rows()."<br/>";

//字段信息
$result = $stmt->result_metadata();
while ($field = $result->fetch_field()) {
    echo $field->name;
}
echo "<br/>";

while ($stmt->fetch()) {
    echo $names,$pwd,"<br/>";
}
$stmt->free_result();
$stmt->close();
$mysqli->close();

@header('Content-Type:text/html;charset=utf-8');
$mysqli = new mysqli("localhost","root","root","test");
/*//预处理  $insertid = $stmt->insert_id;新增id
$sql = "update user1 set names=?,pwd=? where id = ?";
$stmt = $mysqli->prepare($sql);
//绑定参数 i(integer) d(double) s(string) b(binary)
$stmt->bind_param("ssi",$names,$pwd,$id);
$id = 1;
$names = "cccc";
$pwd = "zzz";
$stmt->execute();
$id = 2;
$names = "dddddd";
$pwd = "zzz1";
$stmt->execute();*/

$sql = "select names,pwd from user1 where id > ?";

$stmt = $mysqli->prepare($sql);
$stmt->bind_param("i",$id);
$stmt->bind_result($names,$pwd);
$id = 0;
$stmt->execute();
/*$stmt->fetch();
echo $names,$pwd;
$stmt->fetch();
echo $names,$pwd;*/
$r = $stmt->store_result();//取出所有
echo "记录总数:".$stmt->num_rows()."<br/>";

//字段信息
$result = $stmt->result_metadata();
while ($field = $result->fetch_field()) {
    echo $field->name;
}
echo "<br/>";

while ($stmt->fetch()) {
    echo $names,$pwd,"<br/>";
}
$stmt->free_result();
$stmt->close();
$mysqli->close();



pdo 方法


class dbPDO{
 public $pdodb;//数据库对象
 public $query;
 public $sqls;//执行的sql语句
 public $table;//
 public $sql;//当前sql
 function __construct($dbarr=array()){   
    $host = $dbarr['host'] ? $dbarr['host'] : SAE_MYSQL_HOST_M;
    $port = $dbarr["port"] ? $dbarr["port"] : SAE_MYSQL_PORT;
    $dbname = $dbarr["dbname"] ? $dbarr["dbname"] : SAE_MYSQL_DB;
    $user = $dbarr["user"] ? $dbarr["user"] : SAE_MYSQL_USER;
    $pwd = $dbarr["pwd"] ? $dbarr["pwd"] : SAE_MYSQL_PASS;
    $this->pdodb = new PDO("mysql:host={$host}:{$port};dbname={$dbname}",$user,$pwd);
 }
 //查询
 function query($sql){      
  $this->query = $this->pdodb->query($sql);
  $this->sqls[] = $sql;
  $this->sql = $sql;
  return $this->query;
 }
 //执行
 function exec($sql){
  $this->sqls[] = $sql;
  $this->sql = $sql;
  return $this->pdodb->exec($sql);
 }
 //最后id
 function lastInsertId(){
  return $this->pdodb->lastInsertId();
 }
 //查询所有
 function fetchAll($sql){
  $query = $this->query($sql);
  return $query->fetchAll();
 }
 //查询一条
 function fetch($sql){
  $query = $this->query($sql);
  return $query->fetch();
 }
 ###############################扩展:自定义#######################
 /**
  * 查询总数...
  * $param["table"] = "table_name";
  * $param["where"] = "id=1";
  */
 function fetchSum($param=array()){
     $where = $param['where'] ? " where ".$param['where'] : null;
     $table = $param['table'] ? $param['table'] : $this->table;
     $sql = "select count(*) as sum from {$table} $where limit 1";
     return $this->fetchRow($sql);
 }
 /**
  * 查询单条记录...
  * $param["field"] = "id,uname";默认*;
  * $param["table"] = "table_name";
  * $param["where"] = "id=1";
  *
  */
 function fetchRow($param=array()){
     $field = $param['field'] ? $param['field'] : "*" ;
     $where = null;
     if($param['where']){
         $param['where'] = $param['where'] > 0 ? "id=".$param['where'] : $param['where'];
         $where =  " where ".$param['where'];
     }
     $table = $param['table'] ? $param['table'] : $this->table;
    $sql = "select $field from {$table} {$where} limit 1";    
    return $this->fetch($sql);
 }
 
 /**
  * 查询多条数据...
  *
  * @param array
  * $param['field'] = 'id,uname';默认*
  * $param['table'] = 'table_name';
  * $param['where'] = 'id<100';
  * $param['limit'] = '0,10';默认1
  * $param['key'] = 'uname';以uname作为索引返回结果
  */
 function fetchArray($param=array()){
     $field = $param['field'] ? $param['field'] : "*" ;
     $where = $param['where'] ? " where ".$param['where'] : null;
     $limit = $param['limit'] ? " limit ".$param['limit'] : null;
     $table = $param['table'] ? $param['table'] : $this->table;    
    $sql = "select $field from {$table} {$where} {$limit}";    
    $res = $this->fetchAll($sql);
    if(is_array($res) && $param['key']){
        $result = array();
        foreach ($res as $k=>$v){
            $result[$v[$param['key']]] = $v;
        }
        return $result;
    }
    return $res;
 }
 /**
  *删除数据...
  *
  * $param['table'] = 'table_name';
  * $param['where'] = 'id<100';
  * $param['limit'] = '0,10';默认所有
  */
 function delete($param=array()){
     $where = null;
     if($param['where']){
         $param['where'] = $param['where'] > 0 ? "id=".$param['where'] : $param['where'];
         $where =  " where ".$param['where'];
     }
     $limit = $param['limit'] ? " limit ".$param['limit'] : null;
     $table = $param['table'] ? $param['table'] : $this->table;         
     $sql = "DELETE FROM {$table} {$where} {$limit}";
     $this->exec($sql);
 }
 /**
  * 写入数据...
  * $param['table'] = 'table_name';
  * $param['data'] = array("uname"=>"root","upwd"=>123456);
  */
 function insert($param){
     $table = $param['table'] ? $param['table'] : $this->table;         
     $sql = "insert into {$table} set ";
     $c = count($param['data']);
     if($c > 0){
         $i = 1;
         foreach ($param['data'] as $k=>$v){
             $sql .= "`{$k}`='{$v}'";
             $sql .= $c == $i ? "" : ",";
             $i++;
         }
         return $this->query($sql);
     }
 }
 /**
  * 预处理插入
  * $param['table'] = 'table_name';
  * $param['field'] = array("uname","upwd");
  * $param['data'][] = array("root",123456);
  * $param['data'][] = array("test",123456);
  */
 function insertPre($param=array()){
     $table = $param['table'] ? $param['table'] : $this->table;         
     $sql = "insert into {$table} set ";
     $c = count($param["field"]);
     if($c > 0){
         $i = 1;
         foreach ($param["field"] as $k=>$v){
             $sql .= "`{$v}`=?";
             $sql .= $c == $i ? "" : ",";
             $i++;
         }
        $stmt = $this->pdodb->prepare($sql);
        foreach ($param["data"] as $k=>$v){
            $stmt->execute($v);
        }
     }
 }
 /**
  * 修改数据...
  * $param['table'] = 'table_name';
  * $param['data'] = array("uname"=>"root","upwd"=>123456);
  * $param['where'] = 'id<100';
  * $param['limit'] = '0,10';默认所有
  */
 function update($param=array()){     
     $table = $param['table'] ? $param['table'] : $this->table;
     $where = null;
     if($param['where']){
         $param['where'] = $param['where'] > 0 ? "id=".$param['where'] : $param['where'];
         $where =  " where ".$param['where'];
     }
     $limit = $param['limit'] ? " limit ".$param['limit'] : null;
     $sql = "update {$table} set ";
     $c = count($param['data']);
     if($c > 0){
         $i = 1;
         foreach ($param['data'] as $k=>$v){
             $sql .= "`{$k}`='{$v}'";
             $sql .= $c == $i ? "" : ",";
             $i++;
         }
         $sql .= $where.$limit;
         return $this->query($sql);
     }
 }
}

普通方法


class dbMysql{
 public $link;//MySQL 服务器的连接 
 public $query;//MySQL 查询 
    /**
     * 实例化...
     * $array 配置信息    
     * @param unknown_type $dbuser 用户名
     * @param unknown_type $dbpw 密码
     * @param unknown_type $dbname 数据库
     * @param unknown_type $dbcharset 编码
     */
 function __construct($array=null) {
  //服务器
  $dbhost = $array['host'] ? $array['host'] : "127.0.0.1";
  $dbuser = $array['user'] ? $array['user'] : "root";
  $dbpwd = $array['pwd'] ? $array['pwd'] : "root";
  $dbname = $array['dbname'] ? $array['dbname'] : 'test';
  $dbcharset = $array['charset'] ? $array['charset'] : "gbk";
  //打开一个到 MySQL 服务器的连接
  $this->link = mysql_connect($dbhost, $dbuser, $dbpwd) or die("Could not connect: " . mysql_error());
  if($this->link){     
   //选择 MySQL 数据库
   if($dbname) {
    mysql_select_db($dbname, $this->link) or die ('Can\'t use foo : ' . mysql_error());
   }   
   //设置编码
      if($dbcharset){
       $this->query("set names {$dbcharset}");
      }
  }
 }
 /**
  * 执行sql...
  *
  * @param unknown_type $sql
  * @return unknown
  */
 function query($sql) {
  $this->query = mysql_query($sql) or die("Invalid query: " . mysql_error());  
  return $this->query;
 }
  /**
     * 从结果集中取得一条记录...
     *
     * @param unknown_type $query
     * @return unknown
     */
 function fetch_row($sql,$result_type = MYSQL_ASSOC) {
  $this->query = $this->query($sql);
  return mysql_fetch_array($this->query, $result_type);
 }
    /**
     * 从结果集中取得多条记录
     *
     * @param unknown_type $sql
     * @param unknown_type $result_type 索引类型: MYSQL_ASSOC(字段),MYSQL_NUM(数字) 和 MYSQL_BOTH(都有)
     * @return unknown
     */
 function fetch_array($sql,$result_type = MYSQL_ASSOC){
  $this->query = $this->query($sql);
  $arr = false;
  while($data = mysql_fetch_array($this->query, $result_type)){
   $arr[] = $data;
  }
  return $arr;
 }
 /**
     * 取得上一步 INSERT 操作产生的 ID ...
     *
     * @return unknown
     */
 function insert_id() {
  return mysql_insert_id();
 }
    /**
     * 取得前一次 MySQL 操作所影响的记录行数...
     *
     * @return unknown
     */
 function affected_rows() {
  return mysql_affected_rows();
 } 
    /**
     * 取得结果集中行的数目...
     *
     * @param unknown_type $query
     * @return unknown
     */
 function num_rows() {
  return mysql_num_rows($this->query);
 }
    /**
     * 取得结果集中字段的数目
     *
     * @return unknown
     */
 function num_fields() {
  return mysql_num_fields($this->query);
 }
    /**
     * 从结果集中取得列信息并作为对象返回...
     *
     * @return unknown
     */
 function fetch_fields() {
  return mysql_fetch_field($this->query);
 }
    /**
     * 取得 MySQL 服务器信息...
     *
     * @return unknown
     */
 function version() {
  return mysql_get_server_info($this->link);
 }
 /**
     * 释放结果内存...
     *
     * @return unknown
     */
 function free_result() { 
  $this->query && mysql_free_result($this->query);
 }
    /**
     * 关闭 MySQL 连接...
     *
     * @return unknown
     */
 function close() {
  $this->link && mysql_close($this->link);
 }
}
$db = new dbMysql(array('dbname'=>'bbs'));
$db->query("insert into face_group set names='1111',sortid='1234'");
$id = $db->insert_id();
var_dump($id);
$db->close();
?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值