mysqli_fetch:返回一行

时间:2013-02-12 02:09:15

标签: php jquery mysql json mysqli

我正在创建一个私人消息系统,我在mysqli_fetch()语句中使用while函数来返回与查询关联的所有行。但是,PHP仅返回MYSQL中的最后一行。

这是我的代码:

<?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);
   ?>

1 个答案:

答案 0 :(得分:3)

它返回所有行,但在while循环中,您始终会覆盖$MText变量。因此,只有最后一个将显示json_encode

也许你的意思是写$MText['T'][]而不是$MText['T']

相关问题