2012-06-30

database engine not supported with auto increment

I discover!! There is some DBengine that's not supported to autoincrement. This is will be a trouble if you script using mysql then suddenly must change into other DB Engine that's not supported auto increment. I discover this when I see PEAR option that's not include read new ID!!? 
In next script or perhaps in your future, we may not deal with DBE (database Engine) we know or perhaps we will using complex DBE (Oracle for example). For this matter, you need to using PEAR or perhaps create a system that make you able to change the config but not all you script.



Let's begin with the solution for DBE not supported with auto number. First I created 1 table called as my_id contain only

  • id
then I create a function that read the table add 1 then return the number. This function is same as function new id in PEAR. This is the function, you can edit to make sure is fit in your DBE.
I'm using mysql, then this script would be like this, if you using other.. can you share to me? just type in codepaste.net ... because sharing is good for you and me!
  
<?php

function newId()
{
/*
view the last value
*/
 $s='select id from my_id order by desc limit 1';
 $q=mysql_query($s);
 $r=mysql_fetch_array($q);
 $id=$r[0]+1;
/*
view the last value
*/
 $s="UPDATE  `my_id` SET  `id` =  '$id'";
 $q=mysql_query($s);
 return $id;
}
 
Actualy this is not function I used.. If you want to have a better table plus contain log .. Please view the next article

Tidak ada komentar: