4
The data of data php. file database does not return, if I change the whole data php. for any html text it returns running long Polling, but if I try to use mysqli it returns nothing.
What can I do to fix this problem ?
It is not SELECT error because if you open the data php. separate it returns the data.
index php.
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript" src="client.js"></script>
</head>
<body>
<h3>Conteúdo</h3>
<div id="response"></div>
</body>
</html>
php server.
<?php
// arquivo cujo conteúdo será enviado ao cliente
$dataFileName = 'data.php';
while ( true )
{
$requestedTimestamp = isset ( $_GET [ 'timestamp' ] ) ? (int)$_GET [ 'timestamp' ] : null;
// o PHP faz cache de operações "stat" do filesystem. Por isso, devemos limpar esse cache
clearstatcache();
$modifiedAt = filemtime( $dataFileName );
if ( $requestedTimestamp == null || $modifiedAt > $requestedTimestamp )
{
$data = file_get_contents( $dataFileName );
$arrData = array(
'content' => $data,
'timestamp' => $modifiedAt
);
$json = json_encode( $arrData );
echo $json;
break;
}
else
{
sleep( 2 );
continue;
}
}
?>
client js.
function getContent( timestamp )
{
var queryString = { 'timestamp' : timestamp };
$.get ( 'server.php' , queryString , function ( data )
{
var obj = jQuery.parseJSON( data );
$( '#response' ).html( obj.content );
// reconecta ao receber uma resposta do servidor
getContent( obj.timestamp );
});
}
$( document ).ready ( function ()
{
getContent();
});
data php.
<?php
$conexao = mysqli_connect ("localhost","usuario","senha","db");
$resulta = mysqli_query ($conexao,"SELECT * FROM nome");
while ($exibe = mysqli_fetch_array($resulta)){
?>
<?php echo $exibe['nomes']; ?>
<?php } ?>
Does a javascript error appear in the browser console? in data.php avoid using the php closing tag. related
– rray
@rray The Code is all ok working perfectly, it seems that long Polling does not update php code only texts, if you test using some text in the date.php vera that works very well long Polling, but if using a mysql or php function no longer takes.
– Josimara