开发者

Query runs in phpmyadmin but not through application

开发者 https://www.devze.com 2023-01-27 01:05 出处:网络
Hi im having an interesting problem. Update: From the comments im getting below this seems to be an issue of trying to run multiple queries at the same time. I had assumed that since I was doing it

Hi im having an interesting problem.

Update: From the comments im getting below this seems to be an issue of trying to run multiple queries at the same time. I had assumed that since I was doing it in phpmyadmin, then it was all good. How does one go about running multiple queries? I am using mysqli? I am using mysql 5.x and using mysqli

Im running my query from my application and if the query does not function I echo it to the screen. Along with is's error

When i run my application I get my query printed to the screen and this error.

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 'ALTER TABLE tmp_bulletins ADD INDEX (bb_id); ' at line 13

When I copy the query which was echoed to the screen and run it in phpmyadmin it works fine every time. I have then tried taking the query that was echoed to the screen and running that on the database through my application and it doesn't work.

As far as I can tell, the exact same query(the one printed to my screen) is working through phpmyadmin, but not through my applcation.

Is there any reason that this might happen? Does phpmyadmin make some changes to a query before it runs it on the database?

Here is the query

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;

And the query that it produces(editing the look as we speak)

    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,c开发者_开发问答ategory,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


Take out the semicolon.


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.

are you executing multiple queries at once?

EDIT

Your queries are complex, simplify it and run query by query. To identify the problem.


It would be good if you could show the whole query.

Do you use multi-queries? If yes, this will work in PMA, but without PMA it depends on mysql-version and -settings, if it could work.

0

精彩评论

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