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

发布于 2024-10-04 22:58:37 字数 4052 浏览 0 评论 0原文

嗨,我有一个有趣的问题。

更新:从我下面得到的评论来看,这似乎是尝试同时运行多个查询的问题。我以为既然我是在 phpmyadmin 中做的,那么一切都很好。如何运行多个查询?我正在使用mysqli?我正在使用 mysql 5.x 并使用 mysqli

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

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

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

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

据我所知,完全相同的查询(打印到我的屏幕上的查询)正在通过 phpmyadmin 运行,但不是通过我的应用程序运行。

有什么原因可能会发生这种情况吗? 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

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,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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

溺孤伤于心 2024-10-11 22:58:37

去掉分号。

Take out the semicolon.

浅忆 2024-10-11 22:58:37

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

您是否同时执行多个查询?

编辑

您的查询很复杂,请简化它并逐个查询地运行查询。找出问题所在。

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.

踏雪无痕 2024-10-11 22:58:37

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

你使用多重查询吗?如果是,这将在 PMA 中工作,但如果没有 PMA,它取决于 mysql-version 和 -settings,如果它可以工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文