php while 循环中的减法
我想我距离 PHP 新手还很远,但由于某种原因我无法让以下内容工作。我正在尝试使用 PHP while 循环从数字中减去一个值。
代码如下,但它似乎只是减去第一个值。
我显然已经启动了一个 mysql 查询,然后有以下代码:
$scMins = 500;
while($f=mysql_fetch_object($r)){
$duration = $f->duration;
$scRem = ($scMins - $duration);
}
return $scRem;
但它只是不工作。
任何帮助将不胜感激。
谢谢, 缺口
I'd like to think I'm far from a PHP novice, but for some reason I cannot get the following to work. I'm trying to subtract a value from a number using a PHP while loop.
The code is below, but it only seems to be subtracting the first value.
I've obviously initiated a mysql query, and then have the following code:
$scMins = 500;
while($f=mysql_fetch_object($r)){
$duration = $f->duration;
$scRem = ($scMins - $duration);
}
return $scRem;
but its just notn working.
Any help would be MUCH appreciated.
Thanks,
Nick
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您总是从初始值 scMins 中减去
You always subtract from the initial value scMins
$r 是什么?
如果它没有返回任何内容,那么很可能意味着您的循环是错误的。我会检查您的查询是否确实返回了某些内容(或者是否错误),问题很可能就在那里。
What is $r?
If it returns nothing, then it most likely means that your loop is wrong. I'd check if your query actually returns something (or if it errors), the problem most likely lies there.
在 while 循环中,变量 $scRem 在每次迭代时都会被覆盖。尝试一下,
但老实说,目前还不清楚您想要实现什么目标,所以这可能是错误的答案;-)
In your while-loop the variables $scRem gets overwritten at each iteration. Try
But honestly it's not very clear what you are trying to achive, so this may be the wrong answer ;-)