i have an auto incrementing ID number with inserts to the table but i want to rename a photo upload with the id number to match the rest of the data being submitted to the database
is there a quick way of getting this info without having to rely on user intervention.
my way is to read into the database before hand and add 1 onto that number but that seams like a long way round.. im sure php has some shortcut...
php/mysql retreave ID to new inserted row
Started by Trevsweb, Jun 19 2008 07:37 AM
4 replies to this topic
#1
Posted 19 June 2008 - 07:37 AM
#2
Posted 19 June 2008 - 10:52 AM
Trevsweb, on Jun 19 2008, 07:37 AM, said:
i have an auto incrementing ID number with inserts to the table but i want to rename a photo upload with the id number to match the rest of the data being submitted to the database
is there a quick way of getting this info without having to rely on user intervention.
my way is to read into the database before hand and add 1 onto that number but that seams like a long way round.. im sure php has some shortcut...
is there a quick way of getting this info without having to rely on user intervention.
my way is to read into the database before hand and add 1 onto that number but that seams like a long way round.. im sure php has some shortcut...
try this to select from the table:
$sql = mysql_query("SELECT * FROM `tablename` ORDER BY `id`") or die(mysql_error());
NOTE: the ORDER BY `id` is unecessary so you may not need it. but it's a habit for me to type it in.. continuing
$fetch_array = mysql_fetch_array($sql);
now let's turn the $fetch_array['id'] into a variable:
$id = $fetch_array['id'];
then go on to select from a table from that id..
$query = mysql_query("SELECT * FROM `tablename` WHERE `id` = '$id' ORDER BY `id`") or die(mysql_error());
then go on from there... hopefully this helped some.
#3
Posted 19 June 2008 - 12:06 PM
http://dev.mysql.com/doc/refman/5.0/en/get...-unique-id.html
http://dev.mysql.com/doc/refman/5.0/en/inf..._last-insert-id
Those links describe the native mysql method for getting the last inserted id. Using this, it is possible that you will have to add 1 to the id to get the next id in line...
Also, Bigd, you might want to add "LIMIT 1" to the end of that query. It helps performance a little (probably only for large sites, but hey, every little bit helps).
http://dev.mysql.com/doc/refman/5.0/en/inf..._last-insert-id
Those links describe the native mysql method for getting the last inserted id. Using this, it is possible that you will have to add 1 to the id to get the next id in line...
Also, Bigd, you might want to add "LIMIT 1" to the end of that query. It helps performance a little (probably only for large sites, but hey, every little bit helps).
#4
Posted 19 June 2008 - 12:36 PM
ya rc69, I was thinking about that too but I decided, ehh he'll figure it out and if he still ahs problems they can continue to post
but ya add a LIMIT 1 to the $sql variable.... maybe even the $query variable
#5
Posted 27 June 2008 - 06:51 AM
just saying thanks very much 
just a quick update on bigdfbc2008's code i had to add the order filter DESC to get the highest id number in the order as the default setting was to list the tables as acending... might not be everyone but i couldnt work out the other way so i kept it simple
thansk again
just a quick update on bigdfbc2008's code i had to add the order filter DESC to get the highest id number in the order as the default setting was to list the tables as acending... might not be everyone but i couldnt work out the other way so i kept it simple
thansk again
Edited by Trevsweb, 28 June 2008 - 08:22 AM.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users
