gpt4 book ai didi

php - uploadify php mysql - 批量上传和批量插入

转载 作者:行者123 更新时间:2023-11-29 14:49:16 34 4
gpt4 key购买 nike

我正在使用 uploadify 将图片上传到服务器。后端我使用 PHP 脚本。在 PHP 脚本中,除了复制到目标位置之外,我还在数据库中插入文件位置和一些其他详细信息。

问题:-如果我上传 20 个文件,PHP 脚本将被调用 20 次,数据库插入语句将被调用 20 次以插入不同的图像。绝对调用数据库 20 次是低效的方法。有什么方法可以保存文件名(和位置)并仅调用一次插入语句(最后)一次插入所有 20 条记录?

感谢您的帮助

问候

基兰

最佳答案

我推荐的一种方法是通过 mysqli 扩展使用准备好的语句。引用主要优点:

The actual purpose to use a prepared statement in sql is to cut the cost of processing queries; NOT to separate data from query. That's how it's being used w/ php NOW, not how it was designed to be used in the first place. With SQL you cut the cost of executing multiple similar queries down by using a prepared statement.. Doing so cuts out the parsing, validation and most often generates an execution plan for said query up front. Which is why they run faster in a loop, than their IMMEDIATE Query cousins do.

来源:http://us2.php.net/manual/en/mysqli.prepare.php#103730

举个例子来说明如何利用它:

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

if ($stmt = $mysqli->prepare("INSERT INTO `images` (`location`, `size`, `othervalues`) VALUES(?, ?, ?)")) {
foreach($images as $image) {
// s = string value
// i = integer value
$stmt->bind_param("sis", $image['location'], $image['size'], $image['othervalues']);
$stmt->execute();
}
$stmt->close();
}
// Your prepared statement failed, handle the error
else {
}

$mysqli->close();

我建议阅读mysqli::preparemysqli_stmt::bind_param有关该过程如何运作的更多信息。我还建议阅读 prepared statements in the MySQL documentation .

关于php - uploadify php mysql - 批量上传和批量插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6146912/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com