如何在类的方法中从PDO返回LastInsertID [英] How can I return LastInsertID from PDO whin a method of a class
问题描述
我已经创建了一个用于处理MySql查询的小类.
I have create a small class that handles my MySql queries.
但是,现在我正在尝试获取最后插入的ID的值,但它对我来说并不起作用
However, Now I am trying to get the value of the last inserted id but it is not wokring for me
如果我在该类中使用processQuery方法,则可以使用诸如插入/更新/删除之类的任何查询来做一条准备语句
if my processQuery method in the class I do a prepare statement with any query like insert/update/remove
我已添加以下行$ this-> lastInsertId = $ this-> pdo-> lastInsertId;这应该给我最后一个插入的ID并将其存储在名为$ lastInsertId的公共变量中,然后我就可以从外部代码访问它了.
I have added this line $this->lastInsertId = $this->pdo->lastInsertId; which should give me the last inserted Id and stores it in the public variable called $lastInsertId then I can access it from my code outside.
如何获取最后插入的ID以使用该类?
How can I get the last inserted ID to work with this class??
谢谢
这是我的课程
<?php
class connection {
private $connString;
private $userName;
private $passCode;
private $server;
private $pdo;
private $errorMessage;
public $lastInsertId;
private $pdo_opt = array (
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);
function __construct($dbName, $serverName = 'localhost'){
//sets credentials
$this->setConnectionCredentials($dbName, $serverName);
//start the connect
$this->startConnection();
}
function startConnection(){
$this->pdo = new PDO($this->connString, $this->userName, $this->passCode, $this->pdo_opt);
if( ! $this->pdo){
$this->errorMessage = 'Failed to connect to database. Please try to refresh this page in 1 minute. ';
$this->errorMessage .= 'However, if you continue to see this message please contact your system administrator.';
}
}
//this will close the PDO connection
public function endConnection(){
$this->pdo = null;
}
//return a dataset with the results
public function getDataSet($query, $data = NULL)
{
$cmd = $this->pdo->prepare( $query );
$cmd->execute($data);
return $cmd->fetchAll();
}
//return a dataset with the results
public function processQuery($query, $data = NULL)
{
$cmd = $this->pdo->prepare( $query );
$this->lastInsertId = $this->pdo->lastInsertId;
return $cmd->execute($data);
}
//this where you need to set new server credentials with a new case statment
function setConnectionCredentials($dbName, $serv){
switch($serv){
case 'BLAH':
$this->connString = 'mysql:host='.$serv.';dbname='.$dbName.';charset=utf8';
$this->userName = 'BLAH';
$this->passCode = 'BLAH';
break;
default:
$this->connString = 'mysql:host='.$serv.';dbname='.$dbName.';charset=utf8';
$this->userName = 'BLAH2';
$this->passCode = 'BLAH2';
break;
}
}
}
?>
推荐答案
您可以添加如下方法:
public function lastInsertId($name = NULL) {
if(!$this->pdo) {
throw new Exception('not connected');
}
return $this->pdo->lastInsertId($name);
}
它只是 PDO::lastInsertId()
的包装.您将不需要最后一个插入ID的本地副本.如果未连接PDO,将抛出异常.如果适合您的设计,可以将其更改为return FALSE;
.
像这样使用它:
$con = new connection('testdb');
$con->processQuery('INSERT INTO `foo` ....');
$lastInsertId = $con->lastInsertId();
这篇关于如何在类的方法中从PDO返回LastInsertID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!