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  |

次のクエリで、最後の 3 行を選択します。

 SELECT * from mytable ORDER BY ID DESC LIMIT 0,3    

問題: php スクリプトで最後の 3 つの演算子をスクリプトの別の場所に別の順序で「エコー」する必要があるため、それぞれの名前を別の変数に割り当てます。この例では:

  • $name0 = ドロシー
  • $name1 = サイモン
  • $name3 = ジョン

だから私はそれらを(例えば)このようなテキストに入れることができます... 「ドロシーはサイモンの後に手術をした最後の人です. ジョンは今日3人の前に止まった...」

事前にお願いします

4

3 に答える 3

0

結果を配列に入れ、キー01、を使用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";
}
于 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 に答える
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 に答える