开发者

how to add multiple records in mysql using php

开发者 https://www.devze.com 2022-12-17 02:49 出处:网络
howto add multiple records in my开发者_Go百科sql using php. Is using array the only solution to this problem. Because I tried using array but the record that would be added in the database would look

how to add multiple records in my开发者_Go百科sql using php. Is using array the only solution to this problem. Because I tried using array but the record that would be added in the database would look like this: array


You can use a multivalue insert statement (INSERT... VALUES (record1), (record2) etc) (see http://dev.mysql.com/doc/refman/5.1/en/insert.html)

This is how you can construct such a statement from array of records

$values = array();
foreach($records_array as $record)
    $values[] = sprintf("( '%s' , '%s' )", addslashes($record['name']), addslashes($record['email']));
$sql = "INSERT INTO users(name, email) VALUES " . implode(",", $values);
mysql_query($sql);


You need to go through each member of the array:

foreach($array as $record)
 {   ...  }

and perform an INSERT query for each member or - better for performance - insert all records in one statement as outlined in user187291's answer.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号