/    Sign up×
Community /Pin to ProfileBookmark

retrieve an auto_increment value

Shopping cart again…
I’m inserting customer info into a customer table, using an auto_increment field for invoice number.

I’m using the mysql function “last_insert_id()” to retrieve the latest invoice number so I can insert associated purchases with the appropriate invoice number.

So far so good.

But I’d really like to be able to get that “last_insert_id()” number into a php variable so I could echo the customers invoice number and other info back to them for a printable invoice.

So…how to retrieve that last value of the “invoice number” field in the customers table efficiently, or how to store that “last_insert_id()” into a php variable?

thx

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@jpmoriartyNov 18.2004 — $last_isert = mysql_insert_id();
Copy linkTweet thisAlerts:
@DJsACNov 18.2004 — How about something along the lines of:
[code=php]
function next_insert_id($table) {
$query = mysql_query("SELECT id
FROM category
ORDER BY id DESC
LIMIT 1) or die (mysql_error());
$lastid = mysql_fetch_row($query);
$lastid++
return $lastid;
}

echo next_insert_id($mytable);
[/code]


EDIT: yeah ^^ or that :p
Copy linkTweet thisAlerts:
@CODanauthorNov 18.2004 — thank you, mysql_insert_id() is just what I was looking for
×

Success!

Help @CODan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...