Continuously variable read on php page

Hello again, i have table in picture below, that table is reading some values of name “var_x” from my microsoft sql database as you can see in table it is happening every second as data is incoming into database. Simply, database is getting variable every one second and sending it to table.

Problem is that i want continuously reading on my .php page, like when new number get into database to show it on page but i get numbers only when i hit refresh page(F5). So lets say if im doing nothing php would be same as it is on this picture and if i hit refresh page i will pick up all new variables until that moment. so… 216, 217, 218…would show up.

Is there a way how to continuously read database on php without hitting refresh?

There is my .php file code:

<!DOCTYPE html>
<html>
<head>
<title> Table with database</title>
<style type="text/css">
table {
border-collapse: collapse;
width: 100%;
color:#d96459;
font-family: monospace;
font-size: 25px;
text-align: left;	
}
th{
	background-color: #d96459;
	color: white;
}


</style>
</head>
<body>
<table>
<tr>
<th>ID</th>
<th>var_x</th>
<th>TIME</th>
</tr>

<?php
try
{
	
$conn = new PDO("sqlsrv:Server=127.0.0.1,50632;Database=variables_DB","","");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

}
catch(Exception $e)

{
die(print_r( $e->getMessage() ) );
}
$tsql = "SELECT * FROM VARIABLESxtable";
$getResults = $conn->prepare ($tsql);
$getResults->execute();
$results = $getResults->fetchAll (PDO::FETCH_BOTH);

foreach($results as $row){
echo '<tr><td>'.' '.$row['var_ID'].' '.'</td><td>'.' '.$row['var_x'].' '.'</td><td>'.' '.$row['TimeStamp'].' '.'</td></tr>';

}

?>
</table>
</body>
</html>


</table>
</body>
</html>

You should look into polling with ajax or even web sockets which are able to keep a connection live so you get everything instantly.

i dont understand this websockets at all. and about ajax and javascript… do you have some code? or function to implement? how to do that… i got no luck… tried to make new script.js and implement it but no luck.

first i set name for table id=“table_id” and implement
<script rel="script" src="microsoftsql/script.js"></script>
before closing

</body>

function refreshData()
{
$(‘table_id’).load(‘microsoftsql/index.php’);
}
window.setInterval(refreshData, 1000);

That makes no sense as a table is unable to load variable content. You have to use $.get and process the returned data somehow, like rebuilding the table rows.

still got a problem. can you explain this a little bit. im dont know much about php and scripting.

Sponsor our Newsletter | Privacy Policy | Terms of Service