lastInsertId - 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() 堆栈跟踪:在第 24 行的 /home/xxxx/index.php 中抛出 #0 {main}


千万里不及你
浏览 148回答 1
1回答

汪汪一只猫

最后一个 lastInsertId() 是 PDO 类上的一个函数,问题是在你的类中你返回准备函数,认为最好的方法是你必须像这样在函数查询中返回 lastInsertId()//---------------Conseld 正如我所建议的那样,我通过使用两个不同的类和函数来补充。<?php/*CONNECTION WITH DATABASES*/define("SERVER", "localhost");define("BASES", "databases");define("USER", "userbases");define("PASS", "******");/*DATABASE AND FUNCTION CONNECTION CLASS*/class QueryPDO extends PDO{&nbsp; &nbsp; private $conn;&nbsp; &nbsp; public function __construct(){&nbsp; &nbsp; &nbsp; &nbsp; $this->conn = new PDO("mysql:dbname=".BASES.";host=".SERVER, USER, PASS);&nbsp; &nbsp; }&nbsp; &nbsp; /*INSERT, UPDATE AND DELETE FUNCTION*/&nbsp; &nbsp; public function query($rawQuery, $params = array()){&nbsp; &nbsp; &nbsp; &nbsp; $stmt = $this->conn->prepare($rawQuery);&nbsp; &nbsp; &nbsp; &nbsp; $stmt->execute();&nbsp; &nbsp; &nbsp; &nbsp; return $this->conn->lastInsertId();&nbsp; &nbsp; }}/*DATABASE AND FUNCTION CONNECTION CLASS*/class SelectPDO extends PDO{&nbsp; &nbsp; private $conn;&nbsp; &nbsp; public function __construct(){&nbsp; &nbsp; &nbsp; &nbsp; $this->conn = new PDO("mysql:dbname=".BASES.";host=".SERVER, USER, PASS);&nbsp; &nbsp; }&nbsp; &nbsp; /*LISTAR DADOS*/&nbsp; &nbsp; public function query($rawQuery, $params = array()){&nbsp; &nbsp; &nbsp; &nbsp; $stmt = $this->conn->prepare($rawQuery);&nbsp; &nbsp; &nbsp; &nbsp; $stmt->execute();&nbsp; &nbsp; &nbsp; &nbsp; return $stmt;&nbsp; &nbsp; }&nbsp; &nbsp; public function select($rawQuery, $params = array()):array{&nbsp; &nbsp; &nbsp; &nbsp; $stmt = $this->query($rawQuery, $params);&nbsp; &nbsp; &nbsp; &nbsp; $stmt->execute();&nbsp; &nbsp; &nbsp; &nbsp; return $stmt->fetchAll(PDO::FETCH_ASSOC);&nbsp; &nbsp; }}/*INSERT - MAY ALSO USE WITH UPDATE AND DELETE*/$query = "INSERT INTO User(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Nome,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Email&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ) VALUES (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Cledson A Stefanato',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'txt@gmail.com'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );";$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">&nbsp; &nbsp; <?php&nbsp; &nbsp; echo $dados["Nome"]. "<br />";&nbsp; &nbsp; echo $dados["Email"]. "<br />";&nbsp; &nbsp; ?></div><?php } ?>&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP