-3
We have a postgresql query within a javascript function which is as follows:
function valor(){
<?php
$con_string = "host='ip' port=numero dbname='banco' user='usuario' password='***'";
$conn = pg_connect($con_string);
$query="SELECT tubeteira from velocidadereal";
$output=pg_query($conn,$query);
$retorna = pg_fetch_array($output);
$valor = $retorna["tubeteira"];
?>
return <?php echo $valor ?>;
}
setInterval(valor,1000);
But although it updates from 1 to a second the php variable is not updating in the function, so by changing the value in the database of the column where we are performing the select it does not return the new value unless we have refreshed the page.
How can I automatically update?
Important to note that the consultation is feeding a graph.
This PHP query is executed only once, by the server, only on the page load, regardless of whether you call the value() function several times, because it is on the client side. To update the variable you will need to do the query in PHP via ajax. I believe someone will give you an answer with an example. I made the comment just for an initial understanding.
– Joao Paulo
Thanks for the comment. I am waiting for an example.
– Renata
Renata, you put "We have a postgresql query inside an AJAX function which is as follows:" But there is no AJAX function there. I recommend this article to you: https://www.devmedia.com.br/executando-consultas-ao-mysql-com-php-e-ajax/26008
– Jhonny Freire