0

I want to display the last update time & date whenever MySQL tables were updated with the latest changes, I got the below PHP code working in PHP 5 but not the latest version PHP 7 and return an error showing nothing on the web page, trying to fix it but no avail, anyone got any idea what went wrong?

   mysql_connect("localhost", "root", "password") or die(mysql_error());
   mysql_select_db("information_schema") or die(mysql_error());
     $query1 = "SELECT 'UPDATE_TIME' FROM 'TABLES' WHERE 'TABLE_SCHEMA' LIKE 'demo' AND 'TABLE_NAME' LIKE 'usc'";
     $result1 = mysql_query($query1) or die(mysql_error());
      while($row = mysql_fetch_array($result1)) {
       echo "<font color='red'>&nbsp; (Last update : ".$row['UPDATE_TIME'].")</font>";
NotAnerd
  • 145
  • 1
  • 10
great23
  • 1
  • 1
  • Hello. Firstly, in your code, you can see that it is vulnerable to SQL Injection [link](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). Its important to use Prepared statements to avoid SQL Injections. So, in this code **TABLE_SCHEMA** and **TABLE_NAME** are columns from **TABLES**? In general, you would like to update **UPDATE_TIME** every time that this table **TABLES** were update? – Michel Xavier Oct 18 '20 at 01:42
  • thanks for ur input. I got mysql tables name 'demo' and 'usc'; yes i wish to dispay latest time when tables were updated. – great23 Oct 18 '20 at 01:49
  • You can use this php class to convert your `mysql_` functions to `mysqli_` https://www.phpclasses.org/package/9199-PHP-Replace-mysql-functions-using-the-mysqli-extension.html – desbest Oct 18 '20 at 01:50
  • thank you sir, will have a look on your suggestion – great23 Oct 18 '20 at 02:01
  • Please share the full and exact error message – Nico Haase Nov 22 '20 at 18:02

1 Answers1

0

the mysql family of functions is removed in PHP 7 As far as I know. You may only use mysqli and pdo. I recommend switching to PDO, as it has more flexibility. See the mysql requirements. Assuming that you have transitioned to PDO, you can use the following code to achieve the same thing:

//select the appropriate database
$pdo_object->query('use information_schema');
$query1 = "SELECT 'UPDATE_TIME' FROM 'TABLES' WHERE 'TABLE_SCHEMA' LIKE 'demo' AND 'TABLE_NAME' LIKE 'usc'";
$query1 = $pdo_object->prepare($query1);
$query1->execute();
while($row = $query1->fetchAll(PDO::FETCH_ASSOC)) {
echo "<font color='red'>&nbsp; (Last update : ".$row['UPDATE_TIME'].")</font>";
}

The aforementioned code also mitigates the MySQL Injection vulnerabilities that your code may be exposed to.

NotAnerd
  • 145
  • 1
  • 10