-2

I am making a private message system and I'm using the mysqli_fetch() function in a while statement to return all the rows associated with the query. However, PHP only returns the last row in MYSQL.

Here is my code:

<?php
$Connect = new mysqli("localhost", "root", "", "Data");
error_reporting(E_ALL ^ E_NOTICE);
session_start();
$Val = $_POST['ID'];
$Get = 'SELECT * FROM CMessages WHERE PID="'.$Val.'"';
$Username = $_SESSION['Username'];
$Admin = $_SESSION['Admin'];

    if($Result = $Connect->query($Get))
    {
    while($Row = $Result->fetch_assoc())
    {
        $User = $Row['Username'];
        $Msg = $Row['Msg'];
        $Date = $Row['Date'];
        $ID = $Row['ID'];

        if($User == $Username)
        {
            $MText['T'] = '<div id="Msg">' . $User . ' : ' . $Msg . ' - ' . $Date .' - <a class="TLink" href="MDelete.php?ID='.$ID.'">Delete</a></div>';
        }
        elseif(isset($Admin))
        {
            $MText['T'] = '<div id="Msg">' . $User . ' : ' . $Msg . ' - ' . $Date .' - <a class="TLink" href="MDelete.php?ID='.$ID.'">Delete</a></div>';
        }
        else
        {
            $MText['T'] = '<div id="Msg">' . $User . ' : ' . $Msg . ' - ' . $Date .'</div>';
        } 
    }
    }

   echo json_encode($MText);
   ?>
4

1 回答 1

3

It returns all rows but , in the while loop, you are always overwriting the $MText variable. Therefore only the last one will be displayed with json_encode.

Maybe you meant to write $MText['T'][] instead of $MText['T'].

于 2013-02-12T02:17:43.563 回答