accessing auto increment in database through code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hemashiki
    New Member
    • Aug 2006
    • 34

    accessing auto increment in database through code

    hi all

    i need a help
    here,i want to increment an number in database through php code
    without using auto increment key in database.it must be incremented automatically by inserting the values in front page..
    can anyone help meeeee plz
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    You could query the database for the highest number in the column you want to increment and then manually increment that number before inserting the data.

    [PHP]
    /* -- Table structure --
    CREATE TABLE Table (
    idcol INT NOT NULL Primary Key,
    data VARCHAR(255) NOT NULL
    )
    */
    $RES = mysql_query("SE LECT MAX(idcol) as 'Max' FROM Table");

    if(mysql_num_ro ws($RES) == 1) {
    $ROW = mysql_fetch_ass oc($RES);
    $inc = $ROW['Max'] + 1;

    $RES2 = mysql_query("IN SERT INTO Table(idcol, data) VALUES($inc, 'mydata')";

    if(!!$RES2) {
    echo "Inserted";
    }
    else {
    echo "Failed";
    }
    }

    [/PHP]

    Why don't you want to use Auto_increment tho?

    Comment

    Working...