找到你要的答案

Q:MySQL query returns 0 rows in PHP, actual records in phpmyadmin

Q:MySQL查询返回0行的PHP,在phpMyAdmin的实际记录

I am trying to get rows from table - specific range of rows to be clear. My query looks like this (just part of the code):

SET @i=0;
SELECT * FROM images WHERE (@i:=@i+1) BETWEEN 1 AND 10;

What is does is get row number of rows and returns rows, whose row number is in given range. When I run this query in phpmyadmin, I can see the result of the query (the actual rows in that range), however the number of rows from query is 0. I am pretty sure the problem is with this part

WHERE (@i:=@i+1)

I have searched throught other answers, but the reason was always in php, not the query itself.

我试图从表特定的行范围得到行,以清除。我的查询看起来像这样(只是代码的一部分):

SET @i=0;
SELECT * FROM images WHERE (@i:=@i+1) BETWEEN 1 AND 10;

所做的是获取行数和返回行数,行数在给定范围内。当我运行这个查询的后台,我可以看到查询的结果(在这一范围内的实际行),但是从查询的行数是0。我很肯定这个问题与这部分有关

WHERE (@i:=@i+1)

我在通过其他的答案,但原因总是在PHP中,没有查询本身。

answer1: 回答1:

When you use phpMyAdmin, each query opens a new database connection, and user variables don't persist between connections. Set the variable in a subquery that you join:

SELECT i.*
FROM images AS i
CROSS JOIN (SELECT @i := 0) AS x
WHERE (@i := @i + 1) BETWEEN 1 AND 10;

当你使用phpMyAdmin,每个查询打开一个新的数据库连接,而用户变量不存在之间的连接。设置变量在子查询中,你加入:

SELECT i.*
FROM images AS i
CROSS JOIN (SELECT @i := 0) AS x
WHERE (@i := @i + 1) BETWEEN 1 AND 10;
mysql  phpmyadmin