douyu9159 2010-02-27 20:52
浏览 18
已采纳

Simple Insert适用于phpmyadmin但不适用于php

im trying to insert this query with mysql_query

INSERT INTO um_group_rights (`um_group_id`,`cms_usecase_id`,`um_right_id`) VALUES (2,1,1) ; 
INSERT INTO um_group_rights (`um_group_id`,`cms_usecase_id`,`um_right_id`) VALUES (2,2,1) ; 
INSERT INTO um_group_rights (`um_group_id`,`cms_usecase_id`,`um_right_id`) VALUES (2,3,1) ; 
INSERT INTO um_group_rights (`um_group_id`,`cms_usecase_id`,`um_right_id`) VALUES (2,4,1) ;

and it returns: 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '; INSERT INTO um_group_rights (um_group_id,cms_usecase_id,um_right_id) V' at line 1

it obviously has a problem with the semicolon but i dont understand why. it works without a problem in phpmyadmin. php version is 5.2.6

  • 写回答

2条回答 默认 最新

  • dongxian3574 2010-02-27 20:55
    关注

    Just to be sure : when you are trying to execute these 4 queries from PHP, you're calling mysql_query four times ?

    For instance :

    mysql_query("INSERT INTO um_group_rights (`um_group_id`,`cms_usecase_id`,`um_right_id`) VALUES (2,1,1)");
    mysql_query("INSERT INTO um_group_rights (`um_group_id`,`cms_usecase_id`,`um_right_id`) VALUES (2,2,1)");
    mysql_query("INSERT INTO um_group_rights (`um_group_id`,`cms_usecase_id`,`um_right_id`) VALUES (2,3,1)");
    mysql_query("INSERT INTO um_group_rights (`um_group_id`,`cms_usecase_id`,`um_right_id`) VALUES (2,4,1)");
    


    What I mean is : you cannot send several distinct queries at once, with only one call to mysql_query (quoting, emphasis mine) :

    mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier .

    You have to "separate" your queries -- which is probably something phpMyAdmin does without telling you.

    And, as @Alexandre pointed out in the comments :

    The query string should not end with a semicolon.


    If you are using the mysqli_* functions (and not mysql_*) to access your database, you could try using mysqli_multi_query.

    Unfortunately, there is such function for mysql_*.

    (BTW : the mysql_* API is the old one -- it would be better, especially for a new project, to use mysqli_*)



    Edit after the comment :

    If it's about performances, yes, doing a single call to the database, instead of four successive PHP <-> MySQL calls, could be better.

    In this case, you could try using the insert syntax that allows to insert several lines at once ; see 12.2.5. INSERT Syntax in MySQL's manual (quoting) :

    INSERT statements that use VALUES syntax can insert multiple rows.
    To do this, include multiple lists of column values, each enclosed within parentheses and separated by commas.
    Example:

    INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);
    

    The values list for each row must be enclosed within parentheses.

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

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线