Why my $last_id variable is null always ? i am using this scirpt for ajax..but i tried to excute without my ajax page,it works fine and the output give me
{"data_from_file":"ghjghj","timestamp":1441695735,"msg_id":"1190","reciver_id":"12","sender_id":"10"}

when i want to check my $last_id variable that was null,

<?php
set_time_limit(0);

$con = mysqli_connect('localhost','root','') or die ("Could not connect to the Database");
mysqli_select_db($con,"msg") or die("Cannot find the database");
error_reporting(E_ALL);

$last_id=null;

while (true) {
    $last_ajax_call = isset($_GET['timestamp']) ? (int)$_GET['timestamp'] : null;
    $date = new DateTime();
    clearstatcache();

    $last_change_in_data_file = $date->getTimestamp();
    $last_change_in_data_file;

    if ($last_ajax_call == null || $last_change_in_data_file > $last_ajax_call) {

        /*$mydbid= 10;
        $name=11;*/
        $lastid=mysqli_query($con,"SELECT * FROM massg ORDER BY id DESC LIMIT 1") or die("no work");
        while($row=mysqli_fetch_assoc($lastid))
        {
        $current_last_id=$row['id'];
        }

        if($current_last_id > $last_id && !($current_last_id ==$last_id)){
            $msgs=mysqli_query($con, "SELECT * FROM massg WHERE id=$current_last_id") or die(mysqli_error($con));

            while($row=mysqli_fetch_assoc($msgs)){
                $messg=$row['messg'];
                $reciver_id=$row['reciver'];
                $sender_id=$row['sender'];
            }

            $result = array(
            'data_from_file' => $messg,
            'timestamp' => $last_change_in_data_file,
            'msg_id'=>$current_last_id,
            'reciver_id'=>$reciver_id,
            'sender_id'=>$sender_id
            );
            $json = json_encode($result);
            echo $json; 
        }
        break;
    } else {
        sleep( 1 );
        continue;
    }
    $last_id=$current_last_id;
}

is that bad ideas ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.