dongweicha6077 2009-11-27 02:15
浏览 21
已采纳

PHP MySql一次插入n行?

How can I optimize this code?

$items[0] = "Item-0";
$items[1] = "Item-1";
$items[2] = "Item-2";
$items[3] = "Item-3";
...
$items[n] = "Item-n";

foreach($items as $item) {
    mysql_query("INSERT INTO mytable (item) VALUES ('$item')");
}

The array is just sample and the key point I like to know is how can I insert n items without querying n times?

Thanks.

  • 写回答

3条回答 默认 最新

  • doutiaoku4495 2009-11-27 02:22
    关注

    you can use mulitvalue insert statements

     insert into mytable(item) values('item1'),('item2') etc
    

    http://dev.mysql.com/doc/refman/5.1/en/insert.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于Lammps建模的描述
  • ¥15 #lingo#请问一下为什么会出现以下情况,是因为l第一个值是0的缘故吗?
  • ¥15 设计格雷码同步八进制计数器
  • ¥100 改写matlab程序(关键词-系统对)
  • ¥15 函数信号发生器仿真电路
  • ¥15 Qt的pixmap和image图片加载都导致程序崩溃怎么办
  • ¥15 Kali木马生成问题求解
  • ¥30 求一下解题思路,完全不懂
  • ¥15 C51单片机串口控制JQ6500语音模块
  • ¥30 想给yolo5模型加一个图片识别界面,但是图片还没有检测出来就闪退了