English 中文(简体)
uploadify-php-mysql-批量上传和批量插入
原标题:uploadify php mysql - bulk upload and bulk insert

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

问题:-如果我上传20个文件,PHP脚本会被调用20次,数据库插入语句会被调用二十次,以插入不同的图像。毫无疑问,调用数据库20次是一种效率低下的方法。有没有什么方法可以保存文件名(和位置),并只调用insert语句一次(最后),一次插入所有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
", 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::准备mysqli_stmt::bind_param了解有关流程如何工作的详细信息。我还建议阅读在MySQL文档中准备了语句

问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签