FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


SQL help.





clydejaw
Hey guys,

This may be the easiest problem ever for you SQL experts out there, I'll try to explain it more clearly.

Currently wanting to do an INSERT into the table with an incrementing ID. The problem is not that I can't do it directly in SQL database... we have 3 different computers and each of us have different rows of data on our local machine.

I just want to write a SQL statement that can find the current ID in that table and increment it. This also will be executed on our live server. Kind of a lot of work for something so convoluted, but we would like to keep track of things we do to the database - and this will be executed via php script.

Is there something like a SQL function that is kinda like INSERT INTO table_name ( id, column, column ) VALUES ( GET_CURRENT_ID+1, 'value', 'value' );

Ridiculous, but would like a simple way of doing it rather than pulling out DATA and looping it through it to find the current/latest ID.

Thanks in advance ( could google it, but what's the point of having this community Wink )
sonam
Maybe I don't understand your question right and in that case just ignore this post. But, if you have auto_increment "id" then every new insert in table will get last id+1. In that case you don't need to use php to update id and it is not important what value of id is on your or your friends computer.

In that case you can just use something like that:
Code:
$id= mysql_insert_id();
mysql_query("INSERT INTO $table VALUES ('$id', '$name', '$surname', '$email')") or die(mysql_error());


Sonam
jmraker
mysql_insert_id() returns the last AUTO_INCREMENT value used

If your primary key is numberic you can create or modify the table to have that value be auto incremented where you don't need to specify the value. Somewhere in the table's database record there is a field that contains the value of the highest primary key used and it'll use that number and add 1 to it

You use mysql_insert_id() in case you want to edit the record that was just added without searching for it, or to use the record ID in other database updates

http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html

If you want to get to the auto increment value you'd have to uses the SQL statement 'SHOW TABLE STATUS ' or have access to the information_schema database and look in the AUTO_INCREMENT field in the TABLES table

http://dev.mysql.com/doc/refman/5.1/en/show-table-status.html
clydejaw
Quote:
mysql_insert_id() returns the last AUTO_INCREMENT value used


yeah that worked fine, thanks guys Very Happy
Related topics
Help: SQL error after uploading new files(thread updated)
SQL Tutorial
I can't upload my Database SQL script ! Helllppppp!!!!
CAN ANYBODY HELP ME PLEASE ?!!
Help for sending 2+ cookies in one page.
[php/SQL] Help needed
SQL + help
some pl/sql help please
Error while creating the SQL table pls help
SQL Problem Help
help, SQL Stored procedure - ASTM rouding rule
help me in blind sql
Strange SQL cursor problem. Pls do not ignore if can help.
[TuT]JOOMLA Installation Guide (Less Easy Way)
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.