Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2007
    Location
    London
    Posts
    117

    Unanswered: Retrieve Auto Increment Value

    Hello all,

    I am trying to retrieve rows by their primary key which is an auto increment field. I am using php to echo/print this out but what is echoed/printed is blank for some reason?! So is it not possible to retrive auto increment values? This is my script:

    PHP Code:
    $table $tablearr[now];
    echo (
    "<h2><a href=\"template.php?ussion=\"".$row['id']."&pic=".$table.">".$row['subject']."</a></h2>"); 
    When the above is run I get this html output:

    Code:
    <h2><a href="template.php?ussion="&pic=politics>Why now and why today?</a></h2>
    Its retrieving other things from the database but not the autoincrement?!! Can anyone tell me why?!

    Thanks all, I appreciate any help

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    you should be using the mysql_insert_id function
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Jun 2007
    Location
    London
    Posts
    117
    Quote Originally Posted by r937
    you should be using the mysql_insert_id function
    mysql_insert_id Get the ID generated from the previous INSERT operation

    I don't want the id of te previous insert i made into the database i want the id of any row i choose?! Can this function do that?

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    no, that function is for the id-of-the-row-just-inserted

    the id of any row you choose is always available in a SELECT statement

    is your query actually selecting it?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  5. #5
    Join Date
    Jun 2007
    Location
    London
    Posts
    117
    Quote Originally Posted by r937
    no, that function is for the id-of-the-row-just-inserted

    the id of any row you choose is always available in a SELECT statement

    is your query actually selecting it?
    How embarrasing i wasn't selecting it! I had this:

    $query = sprintf("SELECT subject, content, date, link FROM $table ORDER BY date DESC");

    i've changed it to this and it works!

    $query = sprintf("SELECT id, subject, content, date, link FROM $table ORDER BY date DESC");

    Thanks again r937

    >A question a bit off topic - is it ok to have an auto increment for a primary key for a table or should i create my own id for secuity purposes?

  6. #6
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by compsci
    >A question a bit off topic - is it ok to have an auto increment for a primary key for a table or should i create my own id for secuity purposes?
    please, could you post this question in the Database Concepts and Design forum

    it's a great question, and will generate a bucketsful of replies

    i hope you will enjoy the ride

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  7. #7
    Join Date
    Jun 2007
    Location
    London
    Posts
    117
    Quote Originally Posted by r937
    please, could you post this question in the Database Concepts and Design forum

    it's a great question, and will generate a bucketsful of replies

    i hope you will enjoy the ride

    lol will do now

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •