Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Any Ideas about that ...

0 views
Skip to first unread message

Angelos

unread,
Jun 16, 2005, 4:09:06 AM6/16/05
to
Hello ...

I have a table Products and a table Files.
Each product may have from 0 to 10 or more files associated with it. So it
is a 1 to M relationship...
My problem is that I cannont see how I can associate files to the product at
the time of Product insertion to the DB.

And this is because I don't know the ID taht the product is going to take
before it is inserted into the DB.

So any IDEAS ?
Thanks Angelos


Aggro

unread,
Jun 16, 2005, 11:36:31 AM6/16/05
to
Angelos wrote:

> And this is because I don't know the ID taht the product is going to take
> before it is inserted into the DB.

First insert the product, then get the id for it using last_insert_id()
and then insert rest of the rows, using that id. (C, PHP and other
implementations have direct function for that, with ODBC you need to run
new query to get it).

And the id last_insert_id() returns is safe to use even with many users,
because it is attached to the current connection. So user with another
connection wont get the id you used in another connection.

C:
http://dev.mysql.com/doc/mysql/en/getting-unique-id.html

PHP:
http://fi2.php.net/manual/fi/function.mysql-insert-id.php

ODBC:
http://dev.mysql.com/doc/mysql/en/odbc-and-last-insert-id.html

Angelos

unread,
Jun 16, 2005, 12:13:15 PM6/16/05
to
> And the id last_insert_id() returns is safe to use even with many users,
> because it is attached to the current connection. So user with another
> connection wont get the id you used in another connection.

> PHP:
> http://fi2.php.net/manual/fi/function.mysql-insert-id.php

THanks a lot man ....

Thats great news !!!!


0 new messages