1

我有一个这样的数据库:

  ----------------------------------------------
  | 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 脚本中“回显”我脚本不同位置和不同顺序的最后三个运算符,因此我将每个名称分配给不同的变量。在这个例子中:

  • $name0 = 多萝西
  • $name1 = 西蒙
  • $name3 = 约翰

所以我可以将它们(例如)放在这样的文本中......“多萝西是西蒙之后最后一个进行手术的人。约翰今天在三个之前停止了......”

提前考虑

4

3 回答 3

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;
于 2012-11-13T09:21:30.707 回答
0

将结果放入数组并使用键012来定位它们:

$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";
}
于 2012-11-13T08:10:18.513 回答
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'] . " "; 
} 
于 2012-11-13T08:10:35.017 回答