尝试为此使用 PHP PDO。它使您可以准备一个语句,然后根据需要多次执行它。例子:
$mysql_host = "127.0.0.1";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "myShop";
$dbLink = new PDO("mysql:host=$mysql_host;dbname=$mysql_database;charset=utf8", $mysql_user, $mysql_password, array(PDO::ATTR_PERSISTENT => true));
$dbLink->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
$query = $dbLink->prepare("insert into `images` (`thumb`, `path`) values (?, ?);");
foreach ($names_array as $thumb => $path)
{
$query->execute(array($thumb, $path)); // note the order as they should appear
}
最终,您可以通过以下方式替换最后一条路径:
$query = $dbLink->prepare("insert into `images` (`thumb`, `path`) values (:thumb, :path);");
foreach ($names_array as $thumb => $path)
{
$query->execute(array(":path" => $path, ":thumb" => $thumb)); // no order restriction
}
...并且您不必按照您拥有字段的顺序提供执行数组。
问候 !