我有一个这样的数据库:
----------------------------------------------
| ID | Time | Typeop | Operator |
----------------------------------------------
| 1 | 10:01 | withdrawal | John |
| 2 | 10:01 | deposit | Mike |
| 3 | 10:01 | deposit | Andrew |
| 4 | 10:02 | check | John |
| 5 | 10:02 | withdrawal | Simon |
| 6 | 10:03 | withdrawal | Dorothy |
通过以下查询我选择最后三行:
SELECT * from mytable ORDER BY ID DESC LIMIT 0,3
问题:我需要在PHP脚本中“回显”我脚本不同位置的最后三个运算符以及不同的顺序,因此我将每个名称分配给另一个变量。在这个例子中:
所以我可以将它们(例如)放在这样的文本中......“多萝西是最后一个在西蒙之后进行手术的人。约翰今天在三人面前停了下来......”
提前预订
答案 0 :(得分:0)
将结果放入数组并使用键0
,1
和2
来定位它们:
$operators = array();
$result = $mysqli->query("SELECT Operator from mytable ORDER BY ID DESC LIMIT 0,3");
if($result)
{
while($row = $result->fetch_assoc())
{
$operators[] = $row['Operator'];
}
}
if(count($operators) >= 3)
{
echo htmlentities($operators[0]) . " has been the last to make an operation after " . htmlentities($operators[1]) . ". " . htmlentities($operators[2]) . " today stopped before them";
}
此示例使用MySQLi并假设始终有3条记录。
编辑:任何MySQL库的原理都是一样的,例如使用原生的mysql_ *库:
$operators = array();
$result = mysql_query("SELECT Operator from mytable ORDER BY ID DESC LIMIT 0,3");
if($result)
{
while($row = mysql_fetch_assoc($result))
{
$operators[] = $row['Operator'];
}
}
if(count($operators) >= 3)
{
echo htmlentities($operators[0]) . " has been the last to make an operation after " . htmlentities($operators[1]) . ". " . htmlentities($operators[2]) . " today stopped before them";
}
答案 1 :(得分:0)
//从表
收集数据
$data = mysql_query("SELECT * from mytable ORDER BY ID DESC LIMIT 0,3") or die(mysql_error());
//将" mytable"信息到$ info数组
$info = mysql_fetch_array( $data );
//打印信息
while($info = mysql_fetch_array( $data ))
{
echo "<b>Operator:</b> ".$info['Operator'] . " ";
}
答案 2 :(得分:0)
您需要将结果存储在独立的php数组中
$ops = array();
$result = mysql_query("SELECT Operator from mytable ORDER BY ID DESC LIMIT 0,3");
$i = 0;
if($result)
{
while($row = mysql_fetch_assoc($result))
$ops[$i++] = $row[0];
}
此处$row
每次都会更改,它只会保留最新值,但$ops
会存储所有
现在你可以在任何地方使用这个数组作为你想要的输出(直到你改变数组$ops
),例如。
$st = $ops[0]." has been the last to make an operation after ".$ops[1].". ".$ops[2]." today stopped before the three...";
echo $st;