Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
使用 PHP 的 PDO 和准备好的语句,我如何实现以下内容?
$sql = 'insert into $tablename ($var1, $var2, $var3, ...) VALUES (:placeholder1, :placeholder2, ...)';
谢谢。
这是一种方法:
$sth = $dbh->prepare('INSERT INTO '.$tablename.' ('.implode(',', array_keys($inserting)).') VALUES ('.str_pad('', count($inserting)*2-1, '?,').')'); $sth->execute(array_values($inserting));
where$tablename是表的名称,并且$inserting是一个关联数组,其中键是列的名称,数组的值是要插入的值。
$tablename
$inserting