Get in touch:
01524 851 877
07718 896 553

Get Next Auto Increment Value

Posted on Sep 24 2008

Earlier today I had the need to try and discover the next auto increment value in a MySQL table. I haven’t needed to do this before so I headed over to google to have a look for how to accomplish it.

A lot of information I found said to use a ‘SELECT MAX…’. This would work fine as long as nothing gets deleted from the end of the database. I ended up finding a nice blog entry here that explains exactly how to do it. The following code snippet taken from the site above shows how to get the next value.

$tablename = "tablename";
$next_increment = 0;
$qShowStatus = "SHOW TABLE STATUS LIKE '$tablename'";
$qShowStatusResult = mysql_query($qShowStatus);

$row = mysql_fetch_assoc($qShowStatusResult);
$next_increment = $row['Auto_increment'];

echo "next increment number: [$next_increment]";
?>

Quite a useful piece of info…

Convert MySql timestamp field to Unix Time Stamp

Posted on Mar 20 2008

As anyone who has used MySql and PHP in the past to deal with time and date will probably know MySql timestamp fields are not compatible in anyway with PHP’s date() function. Because of this you need to do conversions in order to use the information from the MySql database. The following function will convert a value from a database into a unix timestamp format for easier manipulation within your PHP code. I have found it quite useful to have around so am putting it up here for others to take advantage of.

/*
* Convert MySql timestamp to unix timestamp
*/
function ts2unix($ts)
{
$year = substr($ts,0,4);
$month = substr($ts,4,2);
$day = substr($ts,6,2);
$hour = substr($ts,8,2);
$minute = substr($ts,10,2);
$second = substr($ts,12,2);
$uts = mktime($hour,$minute,$second,$month,$day,$year);
return ($uts);
}