从服务器自动更新

时间:2014-04-13 22:19:17

标签: javascript php mysql

我正在聊天信使。我有登录用户名和我聊天的人的ID。我写过这个脚本实际上是我复制过的。

<script>
    if(typeof(EventSource) !== "undefined")
    {
        var source = new EventSource("server_update.php");
        source.onmessage = function(event)
        {
            document.getElementById("result").innerHTML = event.data;
        };
    }
    else
    {
        document.getElementById("result").innerHTML = "Sorry, your browser does not support server-sent events...";
    }
</script>

我的服务器端PHP代码是这样的:

<?php
    session_start();
    header('Content-Type: text/event-stream');
    header('Cache-Control: no-cache');
    $con = mysqli_connect("localhost", "root", "root", "messenger_db") or die("could not mysqli_error" .mysqli_error($con));
    $sql = "SELECT * FROM MESSAGES WHERE sent_to='$_SESSION[frnd]' AND sent_from= '$_SESSION[QWERTY]'
             UNION
            SELECT * FROM MESSAGES WHERE sent_to='$_SESSION[QWERTY]' AND sent_from='$_SESSION[frnd]' ORDER BY send_time "; //retrieving message from table $_session is the logged user and $_fns is the person i am talking with
    $result = mysqli_query($con, $sql) or die("problem executing error".mysqli_error($con));
    $ret = '<table>';
    if($result)
    {
        while($row=mysqli_fetch_array($result))
        {
            $ret. = '<tr id="'.$row["id"].'"><td>' . $row['content'] . '</td><td>'.$row['sent_time'].'</td></tr>';
        }
        $ret. = '</table>';
        echo $ret; // echoing the messages that my friend have just send
    }
    else {
        echo "no message ";
        flush();
    }
?>

PHP脚本在获取服务器时间方面运行良好,但这在mysql查询中不起作用。

0 个答案:

没有答案