phpmysql操作函数类(pdo)

注意,经常出现插入数据库数据时,中文字符乱码,这时需要设置一下 pdo 的传输编码。

$pdo->exec("SET CHARACTER SET UTF8");

之前写过一次使用mysql_connect() 进行连接的类,链接为http://blog.csdn.net/leiflyy/article/details/50629657

后来使用了高版本的php和mysql,发现之前那个类过时了,,于是就重写了使用pdo的操作类。

写了两个,一个比较简单。。一个比较复杂。。。。。。doc写的挺详细的,,,我就不解释了。。。

具体使用方法

好了,,,先上简单的。。

<?php
/**
 * Created by PhpStorm.
 * User: leif
 * Date: 2016/5/11 0011
 * Time: 10:13
 */
include_once 'config.php';

/**
 * 连接数据库
 * @param string $dbhost
 * @param string $dbname
 * @param string $username
 * @param string $password
 * @return PDO
 */
function connectMysql($dbhost,$dbname,$username,$password){
    $dsn = "mysql:host={$dbhost};dbname={$dbname}";
    $pdo = new PDO($dsn,$username,$password);
    return $pdo;
}

/**
 * 查询一条数据
 * @param PDO $pdo
 * @param $sql
 * @return mixed|PDOStatement
 */
function fetchOneMysql(PDO $pdo,$sql){
    $row = $pdo->query($sql);
    $row = $row->fetch();
    return $row;
}

/**
 * 查询多条数据
 * @param $pdo
 * @param $sql
 * @return mixed
 */
function fetchAllMysql($pdo,$sql){
    $row = $pdo->query($sql);
    $row = $row->fetchAll();
    return $row;
}

/** 删除一条数据
 * @param PDO $pdo
 * @param $table
 * @param null $where
 * @return int
 */
function deleteMysql(PDO $pdo,$table,$where=null){
    $sql = "delete from {$table} where {$where}";
    $res = $pdo->exec($sql);
    return $res;
}

/**
 * 插入一条数据
 * @param PDO $pdo
 * @param string $table
 * @param array $array
 * @return number
 */
function insertIntoOneMysql(PDO $pdo,$table,$array){
    $keys = join(",", array_keys($array));
    $values = join(",", array_values($array));
    $sql = "insert into `{$table}` ({$keys}) values ({$values})";
    $res = $pdo->exec($sql);
    return $res;
}

/**
 * 修改一条数据
 * @param PDO $pdo
 * @param string $table
 * @param array $array
 * @param string $where
 * @return string
 */
function updateMysql($pdo,$table, $array,$where){
    foreach ($array as $key=>$value){
        if (@$set == null){
            $p = "";
        }else {
            $p = ",";
        }
        @$set .= "{$p}`{$key}`='{$value}'";
    }
    $sql = "update `{$table}` set {$set} ".($where==null?null:"where ({$where})");
    $res = $pdo->exec($sql);
    return $res;
}


然后是复杂的,但是鲁棒性更好

<?php

/**
 * 
 * @author leiflyy@outlook.com
 *
 */
class mysqlcon{
    private $_dbhost;
    private $_dbname;
    private $_username;
    private $_password;
    private $_pdo;

    /**
     * 创建对象是初始化数据库连接信息
     * @param string $dbhost databases-host
     * @param string $dbname databases-name
     * @param string $username databases-username
     * @param string $password databases-password
     */
    function __construct($dbhost,$dbname,$username,$password){
        $this->_dbhost = $dbhost;
        $this->_dbname = $dbname;
        $this->_username = $username;
        $this->_password = $password;
    }

    /**
     * 连接数据库
     * @return string  连接失败会返回异常信息,成功返回成功信息
     */
    function connectMysql(){
        try {
            $dsn = "mysql:host={$this->_dbhost};dbname={$this->_dbname}";
            $pdo = new PDO($dsn,$this->_username,$this->_password);
        } catch (PDOException $e) {
            return "mysql 连接失败".$e->getMessage();
        }
        $this->_pdo = $pdo;
        return "Query OK";
    }

    /**
     * 查询一行数据
     * @param string $sql 查询语句
     * @throws Exception  查询失败,抛出异常
     * @return array 查询成功返回数组,失败返回异常信息
     */
    function fetchOneMysql($sql){
        try {
            $row = $this->_pdo->query($sql);
            if ($row){
                $row = $row->fetch();
            }else {
                throw new Exception("sql语句或pdo对象出错,查询结果为空");
            }           
        } catch (Exception $e) {
            return $e->getMessage();
        }
        return $row;
    }

    /**
     * 查询多行数据
     * @param string $sql 查询语句
     * @throws Exception  查询失败,抛出异常
     * @return array 查询成功返回数组,失败返回异常信息
     */
    function fetchAllMysql($sql){
        try {
            $row = $this->_pdo->query($sql);
            if ($row){
                $row = $row->fetchAll();
            }else {
                throw new Exception("sql语句或pdo对象出错,查询结果为空");
            }
        } catch (Exception $e) {
            return $e->getMessage();
        }
        return $row;
    }

    /**
     * 删除数据
     * @param string $table 要操作的表
     * @param string $where 删除条件 
     * @throws Exception 删除失败,抛出异常
     * @return string 删除成功,返回query ok信息,删除失败,返回异常信息
     */
    function deleteMysql($table,$where=null){
        $sql = "delete from {$table} where {$where}";
        try {
            $res = $this->_pdo->exec($sql);
            if ($res){

            }else {
                throw new Exception("删除失败,请检查传入参数");
            }
        } catch (Exception $e) {
            return $e->getMessage();
        }
        return "Query OK, {$res} row affected";
    }

    /**
     * 插入一条数据
     * @param string $table 要操作的表
     * @param array $array 要插入的数据,以key=>value方式对应存储在数组
     * @throws Exception 插入失败,抛出异常
     * @return string 插入成功,返回query ok信息,插入失败,返回异常信息
     */
    function insertIntoOneMysql($table,$array){
        $keys = join(",", array_keys($array));
        $values = join(",", array_values($array));
        $sql = "insert into `{$table}` ({$keys}) values ({$values})";
        try {
            $res = $this->_pdo->exec($sql);
            if ($res){

            }else {
                throw new Exception("插入失败,请检查参数是否正确");
            }
        } catch (Exception $e) {
            return $e->getMessage();
        }
        return "Query OK, {$res} row affected";
    }

    /**
     * 修改数据
     * @param string $table 要操作的表
     * @param array $array 要修改的数据,以key=>value的方式对应存储在数组中
     * @param string $where 修改条件
     * @throws Exception 修改失败,抛出异常
     * @return string 修改成功,返回query ok信息,修改失败,返回异常信息
     */
    function updateMysql($table, $array,$where){
        foreach ($array as $key=>$value){
            if (@$set == null){
                $p = "";
            }else {
                $p = ",";
            }
            @$set .= "{$p}`{$key}`='{$value}'";
        }
        $sql = "update `{$table}` set {$set} ".($where==null?null:"where ({$where})");
        try {
            $res = $this->_pdo->exec($sql);
            if ($res){

            }else {
                throw new Exception("修改失败,请检查参数");
            }
        } catch (Exception $e) {
            return $e->getMessage();
        }
        return "Query OK, {$res} row affected";
    }
}


?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值