PHP MYSQL SET在while循环中给出错误

时间:2013-04-21 06:07:50

标签: php mysql while-loop set

我有这个问题:

$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中工作正常,而while在没有SET @ total = 0;

的情况下正常工作

2 个答案:

答案 0 :(得分:0)

由于您无法在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

答案 1 :(得分:0)

使用两次拨打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");

我认为变量应该持久化,因为它是相同的数据库连接。