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>";
5
  • Hello. Firstly, in your code, you can see that it is vulnerable to SQL Injection link. 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? Commented Oct 18, 2020 at 1:42
  • thanks for ur input. I got mysql tables name 'demo' and 'usc'; yes i wish to dispay latest time when tables were updated. Commented Oct 18, 2020 at 1:49
  • You can use this php class to convert your mysql_ functions to mysqli_ phpclasses.org/package/… Commented Oct 18, 2020 at 1:50
  • thank you sir, will have a look on your suggestion Commented Oct 18, 2020 at 2:01
  • Please share the full and exact error message Commented Nov 22, 2020 at 18:02

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

thank you sir, let me explore further with your input, it's useful.
My pleasure, if you need help setting up PDO or anything else, feel free to comment.
hi do i need to include mysql_connect in the first line?
as I previously mentioned, mysql is removed in PHP 7, you will need to setup PDO. More info on setting this up can be found here.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.