Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 查询在成功执行()时不会返回true_Php_Pdo - Fatal编程技术网

Php 查询在成功执行()时不会返回true

Php 查询在成功执行()时不会返回true,php,pdo,Php,Pdo,MariaDB服务器版本为10.1.21 下面是我的数据库类,它在User类中用于向DB添加用户。在将用户保存到数据库时,execute方法似乎不会返回true,尽管添加了用户 <?php class Database { private $host = DB_HOST; private $user = DB_USER; private $pass = DB_PASS; private $dbname = DB_NAME; private $db

MariaDB服务器版本为10.1.21

下面是我的数据库类,它在User类中用于向DB添加用户。在将用户保存到数据库时,execute方法似乎不会返回true,尽管添加了用户

<?php

class Database {
    private $host = DB_HOST;
    private $user = DB_USER;
    private $pass = DB_PASS;
    private $dbname = DB_NAME;

    private $dbh;       // DB handler
    private $error;
    private $stmt;

    public function __construct() {
        // set DSN i. e. data source name
        $dsn = "mysql:host=".$this -> host.";dbname=".$this -> dbname;
        // set options
        $options = array(
            PDO::ATTR_PERSISTENT => true,
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
        );
        // create a new PDO instance
        try {
            $this -> dbh = new PDO($dsn, $this -> user, $this -> pass, $options);       // connector to DB
        } catch(PDOException $ex) {
            $this -> error = $ex -> getMessage();
        }
    }

    public function query($query) {
        $this -> stmt = $this -> dbh -> prepare($query);
    }

    public function bind($param, $value, $type = null) {
        if (is_null($type)) {
            switch (true) {
                case is_int($value):
                    $type = PDO::PARAM_INT;
                    break;
                case is_bool($value):
                    $type = PDO::PARAM_BOOL;
                    break;
                case is_null($value):
                    $type = PDO::PARAM_NULL;
                    break;
                default:
                    $type = PDO::PARAM_STR;
            }
        }
        $this -> stmt -> bindValue($param, $value, $type);
    }

    public function execute() {
        $this -> stmt -> execute();
    }

    public function resultset() {
        $this -> execute();
        return $this -> stmt -> fetchAll(PDO::FETCH_OBJ);       // FETCH_OBJ to fetch object from DB. Could be associative array
    }

    public function single() {
        $this -> execute();
        return $this -> stmt -> fetch(PDO::FETCH_OBJ);
    }

    public function rowCount() {
        return $this -> stmt -> rowCount();
    }

    public function lastInsertId() {
        return $this -> dbh -> lastInsertId();
    }

    public function beginTransaction() {
        return $this -> dbh -> beginTransaction();
    }

    public function endTransaction() {
        return $this -> dbh -> commit();
    }

    public function cancelTransaction() {
        return $this -> dbh -> rollback();
    }
}
上述错误情况将在另一个代码段中处理:

if ($user -> register($data)) {
                    redirect('index.php', 'You are registered and can now log in', 'success');
                } else {
                    redirect('index.php', 'Something went wrong with the registration', 'error');
                }
收到的唯一消息是错误消息

请建议。我看不出代码有什么问题。用户添加得很好。

您的数据库执行方法不返回任何结果。这是空的,这是假的。所以添加返回语句:

public function execute() {
    return $this -> stmt -> execute();
}

旁注,但是像$this->stmt这样的空格非常烦人。我很惊讶它居然能起作用。很多人遵循PSR-2的风格:就是这样。非常感谢。
public function execute() {
    return $this -> stmt -> execute();
}