Results 1 to 7 of 7

Thread: getting last record from DB

  1. #1

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Talking getting last record from DB

    I have a field on my page, that i want to be pre filled in.
    I want the field to be prefilled in withthe last "PASC ID" +1 thats in my db

    I have this code:

    <span style="font-size:small;font:sans serif;">PASC Ref #:</span>
    <input type="text" name="pascref" size="20" maxlenght="20" value="<?php
    //not sure what to put here
    I know i want to query my DB, but i dont know how to get the last entry in PASC_ID table in my DB, then ADD 1 to it, so right now the PASC_ID are like "PASC 101", so when they go to the page, i wanted it to go into the db, and get the last value, and in the value of that text field it says "PASC 102"

    ?>" />

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: getting last record from DB

    select max(PASC_ID) + 1 from table_name

  3. #3

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: getting last record from DB

    Will that work, even if the table has words + numbers to it?
    SO right now its "PASC 101" in that table, so if i do the above code it will say "PASC 102"?

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: getting last record from DB

    instead of asking if it should work, didn't you try it? :/ the MySQL function max() returns the highest value found for the given field. if you add one to it, then it will add one to the returned value.

  5. #5

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: getting last record from DB

    Ok i put this in

    PHP Code:
        <input type="text" name="pascref" size="20" maxlenght="20" value="
    <?php  
      $query1 
    "SELECT max(pasc_id) + 1 FROM dss_returns"
      
    $result1 = @mysql_query ($query1);  
      echo 
    $result1
    ?>
        " />
    but i am getting this in my box.. "Resource id #4"
    I dont know why its getting that

  6. #6
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: getting last record from DB

    that's because you have to return a result from your query if you want to get the output given by MySQL. you queried the database, but you didn't fetch anything from that result. you can use mysql_fetch_array() or mysql_fetch_assoc() to do that.

    an example:
    PHP Code:
    <?php
      $query 
    'SELECT max(pasc_id)+1 FROM dss_returns';
      
    $result mysql_query($query);
      
    $assoc mysql_fetch_assoc($result);
      echo 
    '<pre>';
      
    print_r($assoc);
      echo 
    '</pre>';
    ?>
    if you're only returning one value and don't need to further use that query, you could use this shortcut:
    PHP Code:
    <?php
      
    @extract(mysql_fetch_assoc(mysql_query("SELECT (max(pasc_id)+1) as result FROM dss_returns")));
      echo 
    $result;
    ?>

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: getting last record from DB

    You shouldn't store "PASC 101" in a text field as your ID, you should store 101 in a numeric field and then prepend the "PASC " string in your code. Storing it in the database is simply redundant.

Posting Permissions

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



Click Here to Expand Forum to Full Width