ll

<?php

if (empty($_REQUEST[ since_id ])) {
$since_id = 0;
} else {
$since_id = (int)$_REQUEST[ since_id ];
}

//$db = new PDO("pgsql:..." postgres root array(
// PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
// PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
//));

$db = new PDO("pgsql:dbname=postgres;host=localhost" postgres root array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
));


//$conn = "host=127.0.0.1 dbname=postgres user=postgres password=root";
//$db = pg_connect($conn);

set_time_limit(30);
$timeline = retrieve($since_id);
//sleep(15);
//var_dump($timeline);
if (empty($timeline)) {
$db->exec( LISTEN timeline_update );
$result = $db->pgsqlGetNotify(PDO::FETCH_ASSOC 0);
if ($result === false) {
output();
}
$timeline = retrieve($since_id);
}
output($timeline);

//var_dump($timeline);


function retrieve($since_id = 0 $limit = 10) {
global $db;
$limit = 10 ;

// $stmt = $db->prepare("SELECT * FROM timeline WHERE id > 3 ORDER BY id DESC LIMIT 3");
// $stmt = $db->prepare("SELECT * FROM timeline WHERE id > :since_id ORDER BY id DESC LIMIT :limit");
$stmt = $db->prepare("SELECT * FROM timeline WHERE body = 9 ORDER BY created DESC ");
// $stmt->bindValue( :since_id $since_id);
// $stmt->bindValue( :limit $limit);
$stmt->execute();
//$result = pg_query( SELECT * FROM timeline WHERE id > 3 ORDER BY id DESC );

//$fp = fopen( sample.txt a+ );
//fwrite( $fp pg_fetch_row($result) ) ;
//fclose($fp);

//return pg_fetch_row($result) ;
// return $stmt->fetchAll();
$phpline = "" ;

$phparray = $stmt->fetchAll();
foreach ( $phparray as $v) {

// json形式で返さないとエラーになるね
// $phpline .= "<a href= http://yahoo.co.jp >" . $v[ id ] . "][" . $v[ body ] . "][" . $v[ created ] . "]</a>";
// $phpline .= {"first":1 "second":2 "third":3} ;
$phpline .= {"phpline":"<a href=http://yahoo.co.jp>" . $v[id] . "][" . $v[body] . "][" . $v[created] . "]</a>"} ;
}
//echo "aaa[" . $phpline . "]aaa" ;
return $phpline ;


}

function output($val) {
//function output($val = array()) {
// header( Content-type: application/json );
// echo json_encode($val);
echo $val;
exit;
}







wwwwwwwwwwwwwww



$(function() {
var retrieve_and_display = (function() {
var current_id = 0;
var new_line = "" ;


return function() {

setTimeout(function(){

$.get( retrieve.php { since_id: 6 })
.done(function(result) {

if (result.length > 0) {
current_id = result[0].id;

$("li").remove();
$( <li /> ).text(result).prependTo($( #timeline ));

$.each(result function() {
alert(result) ;
$( <li /> ).text(result).prependTo($( #timeline ));

});
}

retrieve_and_display();
});

// 2000ms 待った後に実行されるコールバック
//} 0);
} 2000);



};




})();

retrieve_and_display();




});






wwwwwwwwwwwwwwwwwwww



<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8" />
<script src="//code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>
<script src="pushtest.js" type="text/javascript"></script>
<title>あああああああああ</title>
</head>
<body>

<p>timeline:</p>


<div style="width:500px;">
<ul id="timeline">
</ul>
</div>


</body>
</html>