0

我无法使用$db->lastInsertId();or获取 lastinsertid 值$db->lastInsertId('fid');

$stmt = $db->stmt_init();   

$sql = "INSERT INTO ch_files_details (name,extension,size,parent) VALUES (?,?,?,?)";

$stmt = $db->prepare($sql) or die($db->error());

$stmt->bind_param('ssii', $filename, $extension, $filesize, $parent);  

$stmt->execute();

$fid = $db->lastInsertId();

那里有什么遗漏或错误吗?

我能够插入到表中,但根本无法获得最后插入的 id。

Table Definition: 
`ch_files_details` (
`fid` bigint(20) NOT NULL AUTO_INCREMENT,
`name` text NOT NULL,
`extension` text NOT NULL,
`size` bigint(20) NOT NULL,
`parent` bigint(20) NOT NULL,
PRIMARY KEY (`fid`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=26 ;
4

1 回答 1

2

PDO::lastInsertId在这里不起作用,因为您没有使用 PDO。您上面的代码是 MySQLi 代码,而不是 PDO。使用mysqli::$insert_id.

// This is all MySQLi prepared statement code, not PDO!!!
// PDO uses bindParam(), not bind_param(), doesn't have stmt_init(), and doesn't use the 'ssii' type strings for binding
$stmt = $db->stmt_init();   
$sql = "INSERT INTO ch_files_details (name,extension,size,parent) VALUES (?,?,?,?)";
$stmt = $db->prepare($sql) or die($db->error());
$stmt->bind_param('ssii', $filename, $extension, $filesize, $parent);  
$stmt->execute();

// insert_id is a property of the connection MySQLi object
$fid = $db->insert_id;

查看MySQLi 手册以获取完整的详细信息。在某些时候,您最终进入了错误的文档集。

于 2012-11-17T14:00:14.757 回答