查询在phpmyadmin中运行,但不是通过应用程序运行

时间:2010-11-27 20:59:28

标签: php mysql phpmyadmin

嗨我有一个有趣的问题。

更新:从下面的评论中,这似乎是尝试同时运行多个查询的问题。我曾经假设,因为我是在phpmyadmin中做的,所以一切都很好。如何运行多个查询?我在使用mysqli?我正在使用mysql 5.x并使用mysqli

我从我的应用程序运行我的查询,如果查询不起作用,我将它回显到屏幕。随着is的错误

当我运行我的应用程序时,我的查询被打印到屏幕上并出现此错误。

您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在'ALTER TABLE tmp_bulletins ADD INDEX(bb_id)附近使用正确的语法; '在第13行

 当我复制回显到屏幕并在phpmyadmin中运行的查询时,它每次都能正常工作。然后我尝试将回显到屏幕上的查询并通过我的应用程序在数据库上运行它并不起作用。

据我所知,完全相同的查询(一个打印到我的屏幕上)正在通过phpmyadmin工作,但不是通过我的applcation。

有没有任何理由可能会发生这种情况? phpmyadmin在对数据库运行之前是否对查询进行了一些更改?

这是查询

CREATE TEMPORARY TABLE tmp_bulletins

                   SELECT {$table}.id AS bb_id,count(bb_replies.id) AS num_responses,bb_locations.title AS location,bb_locations.description AS location_description,bb_categories.title AS category,bb_categories.description AS category_description,Concat(users.first_name,' ',users.last_name) AS full_name,users.first_name AS first_name,users.last_name as last_name,users.id AS user_id,{$table}.title AS post_title,{$table}.content,{$table}.created_date,{$table}.rank 
                   FROM `{$table}` 
                   LEFT JOIN bb_locations ON {$table}.bb_locations_id = bb_locations.id
                   LEFT JOIN bb_categories ON {$table}.bb_categories_id = bb_categories.id
                   LEFT JOIN users ON {$table}.users_id = users.id
                   LEFT JOIN bb_replies ON {$table}.id = bb_replies.bbs_id
                   WHERE `bb_locations_id` IN ({$locations_csv}) AND `bb_categories_id` IN ({$categories_csv}) {$addWhere}
                   GROUP BY bb_id,location,location_description,category,category_description,first_name,last_name,user_id,post_title,content,created_date,rank
                   {$order} {$limit} ;

              ALTER TABLE tmp_bulletins ADD INDEX (`bb_id`);


              CREATE TEMPORARY TABLE tmp_ratings      

                   SELECT bbs_id,
                   sum(CASE WHEN user_id = {$user_id} THEN like_dislike_id END) AS thisUsersRating,
                   SUM(CASE WHEN like_dislike_id = 2 THEN 1 ELSE 0 END) AS likes, 
                   SUM(CASE WHEN like_dislike_id = 1 THEN 1 ELSE 0 END) AS dislikes
                   FROM bb_ratings
                   GROUP BY bbs_id;  

              ALTER TABLE tmp_ratings ADD INDEX (`bbs_id`); 

              SELECT * FROM tmp_bulletins
          LEFT JOIN tmp_ratings on tmp_bulletins.bb_id = tmp_ratings.bbs_id;

它产生的查询(在我们说话时编辑外观)

    CREATE TEMPORARY TABLE tmp_bulletins 
    SELECT bbs.id AS bb_id,count(bb_replies.id) AS num_responses, 
    bb_locations.title AS
     location,bb_locations.description AS location_description,
    bb_categories.title AS
     category,bb_categories.description AS category_description,
    Concat(users.first_name,' ',users.last_name) AS full_name,
    users.first_name AS first_name,
    users.last_name as last_name,users.id AS user_id,
    bbs.title AS post_title,
    bbs.content,bbs.created_date,bbs.rank FROM `bbs` 
    LEFT JOIN bb_locations ON bbs.bb_locations_id = bb_locations.id 
    LEFT JOIN bb_categories ON bbs.bb_categories_id = bb_categories.id 
    LEFT JOIN users ON bbs.users_id = users.id LEFT JOIN bb_replies ON bbs.id = bb_replies.bbs_id WHERE `bb_locations_id` IN (1,2) AND `bb_categories_id` IN (1,2) 
    GROUP BY bb_id,location,location_description,category,category_description,first_name,last_name,user_id,post_title,content,created_date,rank LIMIT 0,5; ALTER TABLE tmp_bulletins 
    ADD INDEX (`bb_id`); 

    CREATE TEMPORARY TABLE tmp_ratings 
    SELECT bbs_id, sum(CASE WHEN user_id = 1 THEN like_dislike_id END) AS thisUsersRating, SUM(CASE WHEN like_dislike_id = 2 THEN 1 ELSE 0 END) AS likes,
    SUM(CASE WHEN like_dislike_id = 1 THEN 1 ELSE 0 END) AS dislikes FROM bb_ratings GROUP BY bbs_id; 
ALTER TABLE tmp_ratings ADD INDEX (`bbs_id`); 

    SELECT * FROM tmp_bulletins LEFT JOIN tmp_ratings on tmp_bulletins.bb_id = tmp_ratings.bbs_id

3 个答案:

答案 0 :(得分:2)

取出分号。

答案 1 :(得分:1)

  

mysql_query()向与指定link_identifier关联的服务器上的当前活动数据库发送唯一查询(不支持多个查询)。

您是一次执行多个查询吗?

修改

您的查询很复杂,简化了查询并按查询运行查询。找出问题所在。

答案 2 :(得分:0)

如果您可以显示整个查询,那就太好了。

您使用多次查询吗?如果是的话,这将在PMA中起作用,但是没有PMA,它依赖于mysql-version和-settings,如果它可以工作的话。