简体   繁体   English

有没有使用for循环插入多个查询的替代方法

[英]is there an alternate to using a for loop to insert multiple queries

I want to insert data from an array. 我想从数组中插入数据。 Below is an example situation. 下面是一个示例情况。

I grab all my friends available in friends list (fb) and store them in an array. 我从朋友列表(fb)中获取所有可用的朋友,并将它们存储在数组中。

Now I want to insert their data ( name, fbid, birthday ) into a table. 现在,我想将其数据( name, fbid, birthday )插入表中。

Currently I'm doing this using a for loop below is an example code. 目前,我正在使用for循环执行此操作,以下是示例代码。

<?php

  $friendsname = $_POST['name'];
  $friendsfbid = $_POST['fbid'];
  $friendsbday = $_POST['birthday'];

  for($i<0;count($friendsfbid);$i++){

     $sql_query  = "INSERT INTO table (fbid, name, birthday) VALUES ('$friendsfbid[$i]','$friendsname[$i]','$friendsbday[$i]') ON DUPLICATE KEY UPDATE fbid='$friendsfbid[$i]', name='$friendsname[$i]', birthday='$friendsbday[$i]'";    

  }

?>

Now if I have 300 friends this will loop 300 times. 现在,如果我有300个朋友,这将循环300次。

The more number of friends the more time its going to take to process the data. 朋友越多,处理数据所需的时间就越多。

Is there a way to avoid this or to increase the performance of the code. 有没有一种方法可以避免这种情况或提高代码的性能。 ?

Using PHP with mySQL 在MySQL中使用PHP

Please See this query hope this will be improve our code and speed. 请参阅此查询,希望这将提高我们的代码和速度。

Avoid doing SQL queries within a loop 避免在循环中执行SQL查询

A common mistake is placing a SQL query inside of a loop. 一个常见的错误是将SQL查询放入循环中。 This results in multiple round trips to the database, and significantly slower scripts. 这将导致数据库多次往返,并且脚本速度明显降低。 In the example below, you can change the loop to build a single SQL query and insert all of your users at once. 在下面的示例中,您可以更改循环以构建单个SQL查询并一次插入所有用户。

foreach ($userList as $user) {

  $query = 'INSERT INTO users (first_name,last_name) VALUES("' . $user['first_name'] . '", "' . $user['last_name'] . '")';

  mysql_query($query);

  }

Instead of using a loop, you can combine the data into a single database query. 您可以将数据合并到一个数据库查询中,而不必使用循环。

$userData = array();

foreach ($userList as $user) {

    $userData[] = '("' . $user['first_name'] . '", "' . $user['last_name'] . '")';

}

$query = 'INSERT INTO users (first_name,last_name) VALUES' . implode(',', $userData);

mysql_query($query);

Insert multiple rows at a time. 一次插入多行。 See this example : 这个例子

INSERT INTO example
  (example_id, name, value, other_value)
VALUES
  (100, 'Name 1', 'Value 1', 'Other 1'),
  (101, 'Name 2', 'Value 2', 'Other 2'),
  (102, 'Name 3', 'Value 3', 'Other 3'),
  (103, 'Name 4', 'Value 4', 'Other 4');

Loop through php to generate the multiple rows data and issue a single insert at last appending the multiple rows 遍历php以生成多行数据,并在最后添加多行后发出单个插入

$sql = '';
foreach($friendsfbid as $key => $value){
   $sql .= INSERT INTO table (fbid, name, birthday) VALUES ('$value[$key]','$friendsname[$key]','$friendsbday[$key]') ON DUPLICATE KEY UPDATE fbid='$value[$key]', name='$friendsname[$key]', birthday='$friendsbday[$key]'";
}
mysql_query($sql);

You can stack your sql INSERTs into string and then run them by calling query function only once. 您可以将sql INSERT堆叠为字符串,然后通过仅调用一次查询函数来运行它们。 That should speed the process up. 那应该加快过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM