問題描述
我遇到了一個問題.我的框架在 PHP 5.3.0 上工作得很好.我將 PHP 版本升級到 PHP 5.4.x,并且我的框架的某些部分開始出現(xiàn)一些問題.
I've come across with a problem. My framework was working just fine with PHP 5.3.0. I upgraded my PHP version to PHP 5.4.x and I started to have few issues with some parts of my framework.
PHP版本升級后,PDO lastInsterId()
總是返回0
.
After PHP version upgrade, PDO lastInsterId()
always returns 0
.
我有一個名為 id
的自增字段.它正在將數(shù)據(jù)添加到數(shù)據(jù)庫中,沒有任何問題.
I have auto-increment field called id
.
It is adding the data to database without any problems.
出于某種原因,我一直將 0 作為最后一個插入 id.
For some reason I keep getting 0 as last insert id.
這是我的代碼;
databaseobjects.php
public static function create () {
global $db;
$attributes = self::sanitize(static::$fields);
$sql = "INSERT INTO ".PREFIX.static::$table_name." (";
$sql .= join(", ", array_keys($attributes));
$sql .= ") VALUE (:";
$sql .= join(", :", array_keys($attributes));
$sql .= ")";
return ($db->crudQuery($sql, $attributes)) ? true : false;
}
public static function lastInsertID () {
global $db;
return $db->handler->lastInsertId();
}
database.php
public function crudQuery($sql, $data) {
$sth = $this->handler->prepare($sql);
return $sth->execute($data);
}
首先調(diào)用create()
方法,然后調(diào)用crudQuery()
方法.正如我之前提到的,我可以成功地將數(shù)據(jù)添加到 MySQL 數(shù)據(jù)庫中.不幸的是,當我調(diào)用 lastInsterID()
方法時,它總是返回 0.
First create()
method is called, then crudQuery()
method is called.
As I mentioned before, I can add the data successfully to MySQL database.
Unfortunately when I call lastInsterID()
method, it always returns 0.
如果您能在我使用 SQL 查詢獲得最后一個 ID 之前幫我解決這個問題,我將非常高興 (:
I will be really glad if you can help me out with this problem before I will get the last ID with SQL Query (:
推薦答案
除了 php/PDO 或您的框架中的錯誤之外,還有兩種可能性.lastInsertId()
在與插入不同的 MySQL 連接上調(diào)用,或者您在應(yīng)用程序/框架中生成 id 并插入它,而不是讓 auto_increment 為您生成它.表中哪一列是主鍵/auto_increment?該列是否包含在 create()
函數(shù)的 $attributes
中?
Other than a bug in php/PDO or your framework, there are two possibilities. Either lastInsertId()
is called on a different MySQL connection than the insert, or you are generating the id in your application/framework and inserting it, rather than letting auto_increment generate it for you. Which column in the table is the primary key/auto_increment? Is that column included in $attributes
in your create()
function?
您可以測試 PDO 以確保該部分使用此代碼(在新文件中)正常工作:
You can test PDO to make sure that part is working correctly with this code (in a new file):
// Replace the database connection information, username and password with your own.
$conn = new PDO('mysql:dbname=test;host=127.0.0.1', 'user', 'password');
$conn->exec('CREATE TABLE testIncrement ' .
'(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50))');
$sth = $conn->prepare('INSERT INTO testIncrement (name) VALUES (:name)');
$sth->execute([':name' => 'foo']);
var_dump($conn->lastInsertId());
$conn->exec('DROP TABLE testIncrement');
當我運行這個腳本時,輸出是
When I ran this script, the output was
string(1) "1"
這篇關(guān)于PDO lastInsertId() 總是返回 0的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!