0

Hej,

I'm quite new to PHP and I have a problem with the following script. The idea is to read content from the database and show it in a json format. I have found this on this page:

https://codewithchris.com/iphone-app-connect-to-mysql-database/

<?php
 
// Create connection
$con=mysqli_connect("localhost","***","***","***");
 
// Check connection
if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
 
// This SQL statement selects ALL from the table 'Locations'
$sql = "SELECT * FROM Locations";
 
// Check if there are results
if ($result = mysqli_query($con, $sql))

{
 // If so, then create a results array and a temporary one
 // to hold the data
 //$resultArray = array();
    // $tempArray = array();
 $resultArray = [];
    $tempArray = [];
    
    
 // Loop through each row in the result set
 while($row = $result->fetch_object())
 {
  // Add each row into our results array
  $tempArray = $row;
     array_push($resultArray, $tempArray);
 }
 
 // Finally, encode the array to JSON and output the results
 echo json_encode($resultArray);
}

// Close connections
mysqli_close($con);
?>

It works quite well and displays all the content of my database.

The problem is that if I do changes to my database (by phpMyAdmin) the php script still shows the content like it was before.

Another weird behavior is that, for example if I rename the php. script from "service.php" to "service1.php" and load this it will reflect the changes made to the database.

My questions are:

  1. Is there an possible improvement to the PHP script?
  2. Is there something I doesn't understand when it comes to PHP/update?
  3. Do I have to initiate the PHP file in some way?
  4. Is there something wrong with my HOST?

Thank you

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
sagerchr
  • 3
  • 2
  • The results of your php script are being cached by the browser, so you just need to avoid that cache. Read this: https://stackoverflow.com/questions/13640109/how-to-prevent-browser-cache-for-php-site – Triby Jan 06 '19 at 11:31

1 Answers1

0

It's most probably your PHP files are being cached. Try adding these lines to the beginning of your .php file.

header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate"); 
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); 
header("Expires: Sat, 10 Jul 1990 05:00:00 GMT"); // Date must be in the past

And restart the server

Shehara
  • 101
  • 3