|
-
Nov 9th, 2006, 10:27 AM
#1
Thread Starter
Frenzied Member
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"
?>" />
-
Nov 9th, 2006, 10:32 AM
#2
Re: getting last record from DB
select max(PASC_ID) + 1 from table_name
-
Nov 9th, 2006, 10:49 AM
#3
Thread Starter
Frenzied Member
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"?
-
Nov 9th, 2006, 10:56 AM
#4
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.
-
Nov 9th, 2006, 12:27 PM
#5
Thread Starter
Frenzied Member
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
-
Nov 9th, 2006, 06:17 PM
#6
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;
?>
-
Nov 9th, 2006, 10:16 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|