PHP Classes

extraneous sql call

Recommend this page to a friend!

      Site Change Detection  >  All threads  >  extraneous sql call  >  (Un) Subscribe thread alerts  
Subject:extraneous sql call
Summary:un-needed sql call ?
Messages:2
Author:John Haywood
Date:2014-01-07 13:04:10
Update:2014-01-07 16:11:46
 

  1. extraneous sql call   Reply   Report abuse  
Picture of John Haywood John Haywood - 2014-01-07 13:04:10
First off, great Class, thank you! I'm integrating it into a CMS to help detect unauthorised file changes.

I notice in public function scan() you have this code;
$query = "SELECT MAX(scan_id ) as lastscan FROM `".$this->config['dbtable']."_scan`";
$data = $this->query($query, __LINE__);
$lastscan = $data['0']['lastscan'];
$query = "INSERT INTO `".$this->config['dbtable']."_scan` VALUES (NULL, NULL)";
$scan = $this->query($query, __LINE__);
$query = "SELECT MAX(scan_id ) as lastscan FROM `".$this->config['dbtable']."_scan`";
$data = $this->query($query, __LINE__);
$currentscan = $data['0']['lastscan'];

I can see you are inserting a NULL row to increment the table fileds scan_id and scantime but as scan_id is an autonumber field, I'm not sure the second SQL call to assign $currentscan is even needed since you could probably just do;
//$query = "SELECT MAX(scan_id ) as lastscan FROM `" . //$this->dbconfig['dbtable'] . "_scan`";
// $data = $this->query($query, __LINE__);
// $currentscan = $data['0']['lastscan']; // int
$currentscan = (int) $lastscan +1;

  2. Re: extraneous sql call   Reply   Report abuse  
Picture of Larry Wakeman Larry Wakeman - 2014-01-07 16:11:46 - In reply to message 1 from John Haywood
Actually, it is not a redundant SQL query as you can't count on being incremented by one as a prior scan may of be deleted from the database. If that happens, the deleted auto-increment value is skipped. I should of used LAST_INSERT_ID() but I was under some pressure to complete this as one of my clients who owns a hosting reseller account was getting hit quite hard. It is not serious as the results of the query are in memory at the time of execution and it is designed to run in a cron job.