【问题标题】:PHP MYSQL SET gives error in while loopPHP MYSQL SET 在 while 循环中给出错误
【发布时间】:2013-04-14 06:06:04
【问题描述】:

我有这个问题:

$result2 = mysql_query("SET @total=0;
SELECT *,
@total:= @total+ `companyearned` AS `total`
FROM `recordedhours` WHERE `group` = '$uid'
ORDER BY `unixdate` DESC, `idnum` DESC
LIMIT $from, $max_results");

while ($rowb = mysql_fetch_array($result2)) {
//DO STUFF
}

但是 SET @total=0;使 while 行给我一个错误: 警告:mysql_fetch_array():提供的参数不是有效的 MySQL 结果资源 查询在 phpmyadmin 中运行良好,而在没有 SET @total=0;

的情况下运行良好

【问题讨论】:

标签: php mysql while-loop set


【解决方案1】:

由于您不能在mysql_query() 中使用多个查询,但您可以将两个查询合并为一个。

试试这个查询..

SELECT *,
@total:= @total+ `companyearned` AS `total`
FROM `recordedhours`, (SELECT @total:=0) r WHERE `group` = '$uid'
ORDER BY `unixdate` DESC, `idnum` DESC
LIMIT $from, $max_results

【讨论】:

  • 感谢您为解决该问题提供的帮助,但列出时没有给出正确的结果。我正在尝试列出两个数字。公司的收入和总额。它应该在底部有最旧的结果。这对公司来说工作得很好,但总的顺序是相反的。这就是我得到的:$-23.22 $-23.22; $-20.22 $-43.44; $300.00 $256.56; $-6.00 $250.56; 这就是我想要的:$-23.22 $181.20; $-20.22 $204.42; $300.00 $224.64; $-6.00 -75.36; (which has brought in the total from all pages up until this point)
  • 有什么想法可以改变这个吗?
  • 我决定,因为除了我正在做的事情之外,它还需要它自己的问题:stackoverflow.com/questions/16173444/…
【解决方案2】:

两次调用mysql_query()

mysql_query("SET @total=0");

$result2 = mysql_query("SELECT *,
@total:= @total+ `companyearned` AS `total`
FROM `recordedhours` WHERE `group` = '$uid'
ORDER BY `unixdate` DESC, `idnum` DESC
LIMIT $from, $max_results");

我认为变量应该保持不变,因为它是同一个数据库连接。

【讨论】:

  • 应该是mysql_query("SET GLOBAL @total=0");
  • @Amir 这只是一个会话变量。只有拥有 SUPER 权限的用户才能设置全局变量。
猜你喜欢
  • 2011-07-10
  • 1970-01-01
  • 1970-01-01
  • 2011-11-30
  • 1970-01-01
  • 2016-07-30
  • 2019-07-10
  • 2016-11-27
  • 1970-01-01
相关资源
最近更新 更多