Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.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 在插入操作中,lastInsertId()不处理PDO对象_Php_Sql_Pdo_Last Insert Id - Fatal编程技术网

Php 在插入操作中,lastInsertId()不处理PDO对象

Php 在插入操作中,lastInsertId()不处理PDO对象,php,sql,pdo,last-insert-id,Php,Sql,Pdo,Last Insert Id,我使用以下选项初始化该类: $options = array( PDO::ATTR_PERSISTENT => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ); 那我就这样做 $this->sql = trim($sql); $this->bind = $this->cleanup($bind); $this->error = ""; try { $pdostmt =

我使用以下选项初始化该类:

$options = array(
      PDO::ATTR_PERSISTENT => true, 
      PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
那我就这样做

$this->sql = trim($sql);
$this->bind = $this->cleanup($bind);
$this->error = "";

try {
   $pdostmt = $this->prepare($this->sql);
   if($pdostmt->execute($this->bind) !== false) {
       if(preg_match("/^(" . implode("|", array("select", "describe", "pragma")) . ") /i", $this->sql))
           return $pdostmt->fetchAll(PDO::FETCH_ASSOC);
       elseif(preg_match("/^(" . implode("|", array("delete", "update")) . ") /i", $this->sql))
           return $pdostmt->rowCount();
       elseif(preg_match("/^(" . implode("|", array("insert")) . ") /i", $this->sql))
           return $pdostmt->lastInsertId();
}
这是因为在插入之后,我需要返回最后一个插入的id。 但有人告诉我lastInsertId()不是函数。 所以它不起作用

有什么想法吗?也许是错误的选择?
谢谢

它在
PDO
对象中,而不是
PDO语句中


是的,我注意到。。。。太蠢了。。。对不起,长时间工作会让你失明。。。。。谢谢这是OOP。为什么要把每一点信息都塞进一个函数?为什么不在需要rowCount时单独调用rowCount呢?你是对的,我从一个在线库中获得了代码,我只是在insert中修改了它,如果只是因为需要的话。当我有更多的时间来更新它,使它更苗条,我会这样做;)无论如何,谢谢你的通知!