niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » PHP
acquiring value of primary key from uploaded file
Post new topic   Reply to topic Page 1 of 1 [11 Posts] View previous topic :: View next topic
Author Message
frizzle
*nix forums Guru


Joined: 25 Mar 2005
Posts: 320

PostPosted: Fri Jul 21, 2006 9:48 am    Post subject: Re: acquiring value of primary key from uploaded file Reply with quote

Andy Hassall wrote:
Quote:
On 20 Jul 2006 14:36:19 -0700, "frizzle" <phpfrizzle@gmail.com> wrote:

I assume the result to be equal.
Would mysql_insert_id() still require a DB connection to be open?

Yes, it's a MySQL call.

So you'd have to run another extra call to the database, instead of
getting it from cache ?
Wouldn't that make it slower?
Not quite getting this, but liked to know how it works ...

Interesting question, as the docs don't seem to explicitly state whether it
does or does not.

Tracing network traffic with Ethereal, MySQL client 5.0.22, server version
4.1.7 on another machine, and sleep() calls between each step to make it
obvious what is happening at each stage, shows that calling mysql_insert_id()
does not result in any communication with the server; so the value must be sent
across and stored in the client as part of running the previous mysql_query().

Looking at the individual packets, this is indeed the case; the ID is returned
as part of the payload in the "OK" packet in reply to the query:


No. Time Source Destination Protocol Info
12 0.006948 192.168.1.101 192.168.1.100 MySQL Request
Command: Query : insert into t (id) values (null)

Frame 12 (91 bytes on wire, 91 bytes captured)
Ethernet II, Src: SC&C_d5:81:1c (00:00:21:d5:81:1c), Dst: SC&C_d5:81:1a
(00:00:21:d5:81:1a)
Internet Protocol, Src: 192.168.1.101 (192.168.1.101), Dst: 192.168.1.100
(192.168.1.100)
Transmission Control Protocol, Src Port: 3693 (3693), Dst Port: 3306 (3306),
Seq: 86, Ack: 83, Len: 37
MySQL Protocol
Packet Length: 33
Packet Number: 0
Command
Command: Query (3)
Parameter: insert into t (id) values (null)

No. Time Source Destination Protocol Info
13 0.008511 192.168.1.100 192.168.1.101 MySQL Response
OK

Frame 13 (65 bytes on wire, 65 bytes captured)
Ethernet II, Src: SC&C_d5:81:1a (00:00:21:d5:81:1a), Dst: SC&C_d5:81:1c
(00:00:21:d5:81:1c)
Internet Protocol, Src: 192.168.1.100 (192.168.1.100), Dst: 192.168.1.101
(192.168.1.101)
Transmission Control Protocol, Src Port: 3306 (3306), Dst Port: 3693 (3693),
Seq: 83, Ack: 123, Len: 11
MySQL Protocol
Packet Length: 7
Packet Number: 1
Response Code: 0
Payload: \001\017\002


In this case the ID was 15 - which is octal \017 - the middle byte in the
payload of the response to the query.

--
Andy Hassall :: andy@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

Wow! Thanks for the extensive information. I understand your
conclusion, but the rest is unfortunately just a load of jibberish (to
technical) to me ... :(

Frizzle.
Back to top
Andy Hassall
*nix forums Guru


Joined: 20 Feb 2005
Posts: 874

PostPosted: Thu Jul 20, 2006 10:51 pm    Post subject: Re: acquiring value of primary key from uploaded file Reply with quote

On 20 Jul 2006 14:36:19 -0700, "frizzle" <phpfrizzle@gmail.com> wrote:

Quote:
I assume the result to be equal.
Would mysql_insert_id() still require a DB connection to be open?

Yes, it's a MySQL call.

So you'd have to run another extra call to the database, instead of
getting it from cache ?
Wouldn't that make it slower?
Not quite getting this, but liked to know how it works ...

Interesting question, as the docs don't seem to explicitly state whether it
does or does not.

Tracing network traffic with Ethereal, MySQL client 5.0.22, server version
4.1.7 on another machine, and sleep() calls between each step to make it
obvious what is happening at each stage, shows that calling mysql_insert_id()
does not result in any communication with the server; so the value must be sent
across and stored in the client as part of running the previous mysql_query().

Looking at the individual packets, this is indeed the case; the ID is returned
as part of the payload in the "OK" packet in reply to the query:


No. Time Source Destination Protocol Info
12 0.006948 192.168.1.101 192.168.1.100 MySQL Request
Command: Query : insert into t (id) values (null)

Frame 12 (91 bytes on wire, 91 bytes captured)
Ethernet II, Src: SC&C_d5:81:1c (00:00:21:d5:81:1c), Dst: SC&C_d5:81:1a
(00:00:21:d5:81:1a)
Internet Protocol, Src: 192.168.1.101 (192.168.1.101), Dst: 192.168.1.100
(192.168.1.100)
Transmission Control Protocol, Src Port: 3693 (3693), Dst Port: 3306 (3306),
Seq: 86, Ack: 83, Len: 37
MySQL Protocol
Packet Length: 33
Packet Number: 0
Command
Command: Query (3)
Parameter: insert into t (id) values (null)

No. Time Source Destination Protocol Info
13 0.008511 192.168.1.100 192.168.1.101 MySQL Response
OK

Frame 13 (65 bytes on wire, 65 bytes captured)
Ethernet II, Src: SC&C_d5:81:1a (00:00:21:d5:81:1a), Dst: SC&C_d5:81:1c
(00:00:21:d5:81:1c)
Internet Protocol, Src: 192.168.1.100 (192.168.1.100), Dst: 192.168.1.101
(192.168.1.101)
Transmission Control Protocol, Src Port: 3306 (3306), Dst Port: 3693 (3693),
Seq: 83, Ack: 123, Len: 11
MySQL Protocol
Packet Length: 7
Packet Number: 1
Response Code: 0
Payload: \001\017\002


In this case the ID was 15 - which is octal \017 - the middle byte in the
payload of the response to the query.

--
Andy Hassall :: andy@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Back to top
Jerry Stuckle
*nix forums Guru


Joined: 24 Feb 2005
Posts: 1515

PostPosted: Thu Jul 20, 2006 10:11 pm    Post subject: Re: acquiring value of primary key from uploaded file Reply with quote

frizzle wrote:
Quote:
Jerry Stuckle wrote:

frizzle wrote:

Rik wrote:


Chris wrote:


I believe you're looking for mysql_last_insert_id()


Thanks - that makes sense, I'll give it a try. Hard to find all the
built-in functions - I had seen mysql_insert_id, but hadn't seen this
one.


That's because mysql_last_insert_id() is not a PHP function. Frizzle was
unfortunately on the fritz. mysql_insert_id() it is, which should work

Grtz,
--
Rik Wasmus


I assume the result to be equal.
Would mysql_insert_id() still require a DB connection to be open?

Frizzle.


Yes, it's a MySQL call.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================


So you'd have to run another extra call to the database, instead of
getting it from cache ?
Wouldn't that make it slower?
Not quite getting this, but liked to know how it works ...

Frizzle


Yes, because it isn't returned so there's no cache to store it in.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Back to top
frizzle
*nix forums Guru


Joined: 25 Mar 2005
Posts: 320

PostPosted: Thu Jul 20, 2006 9:36 pm    Post subject: Re: acquiring value of primary key from uploaded file Reply with quote

Jerry Stuckle wrote:
Quote:
frizzle wrote:
Rik wrote:

Chris wrote:

I believe you're looking for mysql_last_insert_id()


Thanks - that makes sense, I'll give it a try. Hard to find all the
built-in functions - I had seen mysql_insert_id, but hadn't seen this
one.


That's because mysql_last_insert_id() is not a PHP function. Frizzle was
unfortunately on the fritz. mysql_insert_id() it is, which should work

Grtz,
--
Rik Wasmus


I assume the result to be equal.
Would mysql_insert_id() still require a DB connection to be open?

Frizzle.


Yes, it's a MySQL call.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

So you'd have to run another extra call to the database, instead of
getting it from cache ?
Wouldn't that make it slower?
Not quite getting this, but liked to know how it works ...

Frizzle
Back to top
Jerry Stuckle
*nix forums Guru


Joined: 24 Feb 2005
Posts: 1515

PostPosted: Thu Jul 20, 2006 9:18 pm    Post subject: Re: acquiring value of primary key from uploaded file Reply with quote

frizzle wrote:
Quote:
Rik wrote:

Chris wrote:

I believe you're looking for mysql_last_insert_id()


Thanks - that makes sense, I'll give it a try. Hard to find all the
built-in functions - I had seen mysql_insert_id, but hadn't seen this
one.


That's because mysql_last_insert_id() is not a PHP function. Frizzle was
unfortunately on the fritz. mysql_insert_id() it is, which should work

Grtz,
--
Rik Wasmus


I assume the result to be equal.
Would mysql_insert_id() still require a DB connection to be open?

Frizzle.


Yes, it's a MySQL call.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Back to top
Rik
*nix forums Guru Wannabe


Joined: 16 Nov 2005
Posts: 291

PostPosted: Thu Jul 20, 2006 8:54 pm    Post subject: Re: acquiring value of primary key from uploaded file Reply with quote

frizzle wrote:
Quote:
Rik wrote:
Chris wrote:
I believe you're looking for mysql_last_insert_id()

Thanks - that makes sense, I'll give it a try. Hard to find all the
built-in functions - I had seen mysql_insert_id, but hadn't seen
this one.


That's because mysql_last_insert_id() is not a PHP function. Frizzle
was unfortunately on the fritz. mysql_insert_id() it is, which
should work

Grtz,
--
Rik Wasmus

I assume the result to be equal.
Would mysql_insert_id() still require a DB connection to be open?

Yes. It's result isn't cached after a query in PHP, but queried from the
database itself.

Grtz,
--
Rik Wasmus
Back to top
frizzle
*nix forums Guru


Joined: 25 Mar 2005
Posts: 320

PostPosted: Thu Jul 20, 2006 6:12 pm    Post subject: Re: acquiring value of primary key from uploaded file Reply with quote

Rik wrote:
Quote:
Chris wrote:
I believe you're looking for mysql_last_insert_id()

Thanks - that makes sense, I'll give it a try. Hard to find all the
built-in functions - I had seen mysql_insert_id, but hadn't seen this
one.


That's because mysql_last_insert_id() is not a PHP function. Frizzle was
unfortunately on the fritz. mysql_insert_id() it is, which should work

Grtz,
--
Rik Wasmus

I assume the result to be equal.
Would mysql_insert_id() still require a DB connection to be open?

Frizzle.
Back to top
Rik
*nix forums Guru Wannabe


Joined: 16 Nov 2005
Posts: 291

PostPosted: Tue Jul 18, 2006 6:27 pm    Post subject: Re: acquiring value of primary key from uploaded file Reply with quote

Chris wrote:
Quote:
I believe you're looking for mysql_last_insert_id()

Thanks - that makes sense, I'll give it a try. Hard to find all the
built-in functions - I had seen mysql_insert_id, but hadn't seen this
one.


That's because mysql_last_insert_id() is not a PHP function. Frizzle was
unfortunately on the fritz. mysql_insert_id() it is, which should work

Grtz,
--
Rik Wasmus
Back to top
Chris
*nix forums beginner


Joined: 24 May 2006
Posts: 13

PostPosted: Tue Jul 18, 2006 3:58 pm    Post subject: Re: acquiring value of primary key from uploaded file Reply with quote

Thanks - that makes sense, I'll give it a try. Hard to find all the
built-in functions - I had seen mysql_insert_id, but hadn't seen this one.

"frizzle" <phpfrizzle@gmail.com> wrote in message
news:1153172829.748180.313090@m79g2000cwm.googlegroups.com...

Quote:

I believe you're looking for mysql_last_insert_id()

Frizzle.
Back to top
frizzle
*nix forums Guru


Joined: 25 Mar 2005
Posts: 320

PostPosted: Mon Jul 17, 2006 9:47 pm    Post subject: Re: acquiring value of primary key from uploaded file Reply with quote

Chris wrote:
Quote:
I have a PHP file upload feature that also gives the user the option to
assign more than 1 project association to the file. The file's name, title,
primary project, etc. is inserted into a document link table in a MySQL db
after the file is uploaded. The secondary (and multiple) project(s) are also
selected using a separate list box (there are 2 boxes - one for the primary
project, and one for any/all associated projects) The selected values from
the secondary box need to be inserted into a junction table that reflects
both the id for the newly uploaded file and the secondary project(s) - with
a primary key for the association itself, i.e,:

assoc_id doc_id proj_id
1 265 5
2 265 3
3 265 1

I would use a foreach statement to pull the projects from the select list,
but how do I get the newly generated doc_id in order to pass that value to
the association table? Can I assign it a variable at the same time I insert
the data into the primary table? The ID is auto-incremented and is inserted
into the primary table as follows:

$tool_insert = "INSERT INTO tools VALUES ('', '$uploadedname', '$tooltitle',
'$tooldesc', '$tooltype_id', '$toolcat_id', '$primeproj_id', '$location')";

Thanks for any help.

Christina

I believe you're looking for mysql_last_insert_id()

Frizzle.
Back to top
Chris
*nix forums beginner


Joined: 24 May 2006
Posts: 13

PostPosted: Mon Jul 17, 2006 9:32 pm    Post subject: acquiring value of primary key from uploaded file Reply with quote

I have a PHP file upload feature that also gives the user the option to
assign more than 1 project association to the file. The file's name, title,
primary project, etc. is inserted into a document link table in a MySQL db
after the file is uploaded. The secondary (and multiple) project(s) are also
selected using a separate list box (there are 2 boxes - one for the primary
project, and one for any/all associated projects) The selected values from
the secondary box need to be inserted into a junction table that reflects
both the id for the newly uploaded file and the secondary project(s) - with
a primary key for the association itself, i.e,:

assoc_id doc_id proj_id
1 265 5
2 265 3
3 265 1

I would use a foreach statement to pull the projects from the select list,
but how do I get the newly generated doc_id in order to pass that value to
the association table? Can I assign it a variable at the same time I insert
the data into the primary table? The ID is auto-incremented and is inserted
into the primary table as follows:

$tool_insert = "INSERT INTO tools VALUES ('', '$uploadedname', '$tooltitle',
'$tooldesc', '$tooltype_id', '$toolcat_id', '$primeproj_id', '$location')";

Thanks for any help.

Christina
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [11 Posts] View previous topic :: View next topic
The time now is Fri Nov 21, 2008 10:42 pm | All times are GMT
navigation Forum index » Programming » PHP
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Running php file everyday on scheduled time sachin PHP 1 Fri Jul 21, 2006 12:49 pm
No new posts Regarding thesaurus iso file Srikanth modules 0 Fri Jul 21, 2006 10:42 am
No new posts how can i get a file descriptor not used? mars system 0 Fri Jul 21, 2006 7:41 am
No new posts small GTK "Open file" dialog David Siroky Debian 0 Fri Jul 21, 2006 7:30 am
No new posts Trouble Declaring 3D Array in Header File free2klim C++ 1 Fri Jul 21, 2006 4:07 am

Loans | Loans | Mortgage | Loans | Budapest
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.3381s ][ Queries: 20 (0.2173s) ][ GZIP on - Debug on ]