PHP确定两个数字之间的数字然后查询 [英] PHP detmine numbers in between two numbers then query
问题描述
这应该很简单,但我想不通
<前><?php$testid = 240;$curid = 251;$cal = $curid - $testid;回声 $cal;?>我想确定其他两个数字之间的数字,因此在此示例中,它检测到 $testid 和 $curid 之间有 11 个数字,但我不需要它来这样做.
我需要它从字面上计算 $curid - $testid 之间的数字,在这个例子中是 241、242、243...一直到 251 然后我需要用这些数字创建一个循环并做一个下面逐个查询
<前>$cal = $curid - $testid;mysql_query("SELECT * FROM wall WHERE id='".$cal."'")//并且对于每个数字,mysql 应该输出带有这些数字的每个数据字段.再次感谢,爱你们.
这里假设 $testid
小于 $curid
:
$testid = 240;$curid = 251;$numbers = range(($testid + 1), ($curid));$query = 'SELECT * FROM wall WHERE id IN (' .implode(', ', $numbers). ')';
如果我 print $query;
我得到:
SELECT * FROM wall WHERE id IN(241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251)
所以就做你的查询:
mysql_query($query);
This should be simple but I can't figure it out
<?php $testid = 240; $curid = 251; $cal = $curid - $testid; echo $cal; ?>
I want to determine the numbers in between two other numbers so for this example it detects there are 11 numbers in between the $testid and $curid, but i dont need it to do that.
I need it to literally figure the numbers in between $curid - $testid which in this example would be 241, 242, 243... all the way to 251 then i need to create a loop with those numbers and do a query below with each one
$cal = $curid - $testid; mysql_query("SELECT * FROM wall WHERE id='".$cal."'") // and for each number mysql should out put each data field with those numbers.
Thanks again, love you guys.
This assumes that $testid
is smaller than $curid
:
$testid = 240;
$curid = 251;
$numbers = range(($testid + 1), ($curid));
$query = 'SELECT * FROM wall WHERE id IN (' . implode(', ', $numbers). ')';
If I print $query;
I get:
SELECT * FROM wall WHERE id IN
(241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251)
So just do your query:
mysql_query($query);
More about range()
and implode()
这篇关于PHP确定两个数字之间的数字然后查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!