Jump to content

Please help improve efficientcy


EchoFool

Recommended Posts

Hey,

 

I have a PHP script which is called very frequently, and would like to make it as efficient as possible - so was wondering if you can give me some points on this script on how i can make it more efficient for my server.

 

<?php
If(!isset($_SESSION['ID'])){
echo '1';
$SELECT = mysql_query("SELECT id from logs WHERE id > '{$_SESSION['ID']}' ORDER BY id DESC LIMIT 1") or die(mysql_error());
$row = mysql_fetch_assoc($SELECT);
  If($row){
$_SESSION['ID'] = $row['id'];
  }
}Else{
$SELECT = mysql_query("SELECT id from logs WHERE id > '{$_SESSION['ID']}' ORDER BY id DESC LIMIT 1") or die(mysql_error());
$row = mysql_fetch_assoc($SELECT);
If($row && $row['id'] > 0){
echo '1';
$_SESSION['ID'] = $row['id'];
}else{
echo $_SESSION['ID'];
    }
}
?>

 

The idea behind it is simply to set the last id of the table to the session. Was hoping some one could see if this is the best way to do it.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/206830-please-help-improve-efficientcy/
Share on other sites

$result = mysql_query("SELECT id FROM logs WHERE id > {$_SESSION['ID']} ORDER BY id DESC LIMIT 1");
if(false !== $result && 0 !== mysql_num_rows($result)) {
  list($id) = mysql_fetch_row($result);
  echo $id;
} else {
  echo $_SESSION['ID'];
}

 

set the last id of the table to the session

 

mysql_query('INSERT INTO ..');
$_SESSION['ID'] = mysql_insert_id();

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.