lastInsertId-PDO

时间:2019-12-15 05:38:18

标签: php pdo

大家好,在插入一条记录后,我想记录寄存器的最后一个ID,我正在使用PDO lastInsertId()并写入$ _SESSION,但是我没有得到我的代码中缺少的内容错误消息

<?php 
session_start();

define("SERVER", "localhost");
define("BASES", "databases");
define("USER", "userbases");
define("PASS", "******");

class Sql extends PDO {
    private $conn;
    public function __construct(){
            try {
                $this->conn = new PDO("mysql:dbname=".BASES.";host=".SERVER, USER, PASS);
        }catch (Exception $e) {
            echo "Database Error: ".$e->getMessage();
    }
        catch(Exception $e){
                echo "Generic error: ".$e->getMessage();
        }
    }

    public function query($rawQuery, $params = array()){
        $stmt = $this->conn->prepare($rawQuery);
        $this->setParams($stmt, $params);
        $stmt->execute();
        return $stmt;
    }

    public function query($rawQuery, $params = array()){
       $stmt = $this->conn->prepare($rawQuery);
       $this->setParams($stmt, $params);
       $stmt->execute();
       return $this->conn->lastInsertId(); //agreement by @dennisgon
   }

}

/* INSERT */

$query = "INSERT INTO User(Name,Email) VALUES ('Cledson Stefanato','teste@gmal.com');";
$txt = new Sql();
$txt->query($query);

$_SESSION["RECORD"] = $txt->lastInsertId();

echo "Recorded: ".$_SESSION["RECORD"];

?>

致命错误:未捕获错误:在/home/xxxx/index.php:24中,对null的成员函数lastInsertId()进行调用:堆栈跟踪:#0 {main}在/home/xxxx/index.php中抛出24

1 个答案:

答案 0 :(得分:0)

last lastInsertId()是PDO类上的一个函数,问题在于您的类中返回了prepare函数,以为最好的方法是必须像这样在函数查询中返回lastInsertId()

// ---------------内容 我建议使用两个不同的类和函数作为补充。

<?php
/*CONNECTION WITH DATABASES*/
define("SERVER", "localhost");
define("BASES", "databases");
define("USER", "userbases");
define("PASS", "******");

/*DATABASE AND FUNCTION CONNECTION CLASS*/
class QueryPDO extends PDO{

    private $conn;
    public function __construct(){
        $this->conn = new PDO("mysql:dbname=".BASES.";host=".SERVER, USER, PASS);
    }
    /*INSERT, UPDATE AND DELETE FUNCTION*/
    public function query($rawQuery, $params = array()){
        $stmt = $this->conn->prepare($rawQuery);
        $stmt->execute();
        return $this->conn->lastInsertId();
    }

}

/*DATABASE AND FUNCTION CONNECTION CLASS*/
class SelectPDO extends PDO{

    private $conn;
    public function __construct(){
        $this->conn = new PDO("mysql:dbname=".BASES.";host=".SERVER, USER, PASS);
    }
    /*LISTAR DADOS*/
    public function query($rawQuery, $params = array()){
        $stmt = $this->conn->prepare($rawQuery);
        $stmt->execute();
        return $stmt;
    }

    public function select($rawQuery, $params = array()):array{
        $stmt = $this->query($rawQuery, $params);
        $stmt->execute();
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

}

/*INSERT - MAY ALSO USE WITH UPDATE AND DELETE*/
$query = "INSERT INTO User(
                Nome,
                Email
                ) VALUES (
                'Cledson A Stefanato',
                'txt@gmail.com'
                );";
$txt = new QueryPDO();

/*SESSION RECORD, LAST REGISTRATION*/
$_SESSION["RECORD"] = $txt->query($query);
echo "Recorded: ".$_SESSION["RECORD"];


/*LIST*/
$query = "SELECT * FROM Cadastro";
$txt = new SelectPDO();
$result = $txt->select($query);

//echo json_encode($result);
//echo var_dump($result);

foreach ($result as $dados){
?>
<div style="color: red; font-size: 15px">
    <?php
    echo $dados["Nome"]. "<br />";
    echo $dados["Email"]. "<br />";
    ?>
</div>
<?php } ?>