2020年9月

PHP版本:7.2
Mysql版本:5.7.31
使用PDO预处理执行insert语句插入数据,prepare执行成功,但execute返回false。

输出PDO错误结果也为空

print_r($pdo->errorCode());
print_r($pdo->errorInfo());

经排查发现主键字段未设置自增.

Db.php

<?php    
use PDO;

class Db
{
/*
PDO对象
*/
    protected static $pdo;

/*
配置数组
*/
    protected static $config;

/*
是否开启了事务
*/
    protected static $transaction = false;

/*

执行语句数
*/
    protected static $transaction_exec = 0;

/*
本次事务执行成功语句数
*/
    protected static $transaction_success = 0;

/*
本次事务执行成功并影响了数据的语句数
*/
    protected static $transaction_valid = 0;

/*
初始化
*/
    protected static function init()
    {
        $config = require('config.php');
        self::$config = $config;
        $pdo = new PDO('mysql:host='. $config['location'] .';port='. $config['hostport'] .';dbname='. $config['database'] .';', $config['username'], $config['password']);
        $pdo -> query('set names '. $config['charset']);
        $pdo->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
        $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
        self::$pdo = $pdo;
        include ROOT_PATH.'Query.php';
    }

/*
制定表全名并开始查询
table:完整的表名
*/
    public static function table($table)
    {
        if(!self::$pdo) self::init();
        $querier = new Query(self::$pdo);
        return $querier->table($table);
    }

/*
指定不带前缀的表名并开始查询
name:不带前缀的表名
*/
    public static function name($name)
    {
        if(!self::$pdo) self::init();
        $querier = new Query(self::$pdo);
        return $querier->table(self::$config['prefix'].$name);
    }

/*
开启事务
*/
    public static function startTrans()
    {
        self::$transaction = true;
        self::$transaction_exec = 0;
        self::$transaction_success = 0;
        self::$transaction_valid = 0;
        self::$pdo->beginTransaction();
        return true;
    }

/*
提交事务并判断是否回滚

mode:事务模式
0、无条件提交
1、有SQL语句执行失败即回滚
2、有写入操作影响行数为0即回滚
*/
    public static function commit($mode = 1)
    {
        self::$transaction = false;
        switch($mode)
        {
            case 0:
                self::$pdo->commit();
                return true;
            case 1:
                if(self::$transaction_exec === self::$transaction_success){
                    self::$pdo->commit();
                    return true;
                }else{
                    $this->rollback();
                    return false;
                }
                break;
            case 2:
                if(self::$transaction_exec === self::$transaction_success){
                    if(self::$transaction_exec === self::$transaction_valid){
                        self::$pdo->commit();
                        return true;
                    }
                }
                $this->rollback();
                return false;
            default:
                $this->rollback();
                return false;
        }
    }

/*
回滚事务
*/
    public static function rollback()
    {
        self::$pdo->rollBack();
        self::$transaction = false;
        return true;
    }

/*
执行语句数 + 1
*/
    public static function incExecNum()
    {
        self::$transaction_exec++;
    }

/*
执行成功语句数 + 1
*/
    public static function incSuccessNum()
    {
        self::$transaction_success++;
    }

/*
执行成功并影响数据的语句数 + 1
*/
    public static function incValidNum()
    {
        self::$transaction_valid++;
    }

    public static function error()
    {
        print_r(self::$pdo->errorCode());
        print_r(self::$pdo->errorInfo());
    }
}

- 阅读剩余部分 -