我需要根据用户ID多次运行数据库查询。以下是我的查询:
$query1="INSERT INTO wp_scloyalty (userid, value) VALUES ('XXXXX', '01234')";我有一个单独查询的用户ID数组,如下所示:
while($rows=mysql_fetch_array($allresult)){
$birthdays_today[] = $rows['user_id'];
}
echo $birthdays_today[0];我想要运行查询,但是在查询中有"userid“= XXXXX,我想用数组中的用户ID填充它。这意味着查询必须运行多次,每次都会将下一个ID输入到查询中。
这有意义吗?
谢谢:)
发布于 2012-03-28 23:53:30
MySQL具有批量插入功能。
$query1="INSERT INTO wp_scloyalty (userid, value) VALUES ('XXXXX', '01234'), ('XXX2', '23234'), ('XXXX3', '3434545')"
<?php
$query = "INSERT INTO wp_scloyalty (userid, value) VALUES ";
$usersToInsert = array();
foreach($birthdays_today as $user_id){
$usersToInsert[] = "($user_id, '01234')";
}
$query .= implode(',', $usersToInsert);
?>发布于 2012-03-28 23:51:55
使用for循环,并使用sprintf()和字符串格式更改查询。
发布于 2012-03-28 23:52:41
<?php
foreach($birthdays_today as $uid){
mysql_query(); // ...
}
?>但
<?php
while($rows=mysql_fetch_array($allresult)){
mysql_query(); // use $rows['user_id'] here
}
?>https://stackoverflow.com/questions/9911213
复制相似问题