This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center
                            
                        
                    
                
                                7年前关闭。
            
                    
我无法使用$db->lastInsertId();$db->lastInsertId('fid');获取lastinsertid值

$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 ;

最佳答案

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;


查看the MySQLi manual以获得完整详细信息。在某些时候,您最终得到了错误的文档集。

关于php - PDO lastInsertId()未获得值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13430951/

10-12 14:38