-
Notifications
You must be signed in to change notification settings - Fork 1
/
long_ajax.php
57 lines (51 loc) · 1.4 KB
/
long_ajax.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
$timeout = 30;
$start_time = time();
function get_msg(){
$con = mysql_connect("localhost", "root", "qwe123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db("web",$con);
$sql = "SELECT * FROM talk";
$result = mysql_query($sql,$con);
return mysql_num_rows($result);
mysql_close($con);
}
// get message
$last_msg = get_msg();
// start the loop
while (true){
// get current time
$current_time = time();
// check if we are timed out
if ($current_time - $start_time > $timeout){
break;
}
// get latest message
$current_msg = get_msg();
if ($last_msg != $current_msg){
$length = $current_msg - $last_msg;
$con = mysql_connect("localhost", "root", "qwe123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db("web",$con);
$sql = "SELECT * FROM talk order by id desc limit $length";
$get = mysql_query($sql);
while($row = mysql_fetch_array($get))
{
echo $row["name"];
echo ",";
echo $row['text'];
echo ",";
echo $row['img'];
echo ";";
}
mysql_close($con);
break;
}
sleep(1);
}