Ryan
Ryan

Reputation: 657

Is there a way using php to take a mysql query result

Is there a way using php to take a MYSQL query result, get a value from a column in the first record fetched, and then put that record back into the query result so when when looping over the result set later, that record can be used again? If this is possible, can I put the record back in the first position again?

Or should I return the value that I need from that first row as a separate variable in my routine in MYSQL. If this would be the better route to take, can someone give me some insight on how to return both a query result set and a separate variable as well? I cannot seem to get this to work either.

Or would the best way to do this be to create my own array from the query result set and then manipulate the mysql result set as need? I'm trying to stay away from this just to cut out the step of creating that array if one of the above two options is possible, otherwise I will just go with this.

Thanks in advance.

Upvotes: 1

Views: 110

Answers (2)

matchdav
matchdav

Reputation: 715

I agree with the answers above but just to state it a slightly different way:

There is no point rewinding a pointer inside the query result object. You have to keep track of where it is, and it's easy to mess up, and it isn't worth the tiny speed increase.

It's much better to make a copy of the entire array and access the records using keys. Much easier to keep track of what is going on.

Upvotes: 0

Reed
Reed

Reputation: 14974

Just before you need to loop through again, you could use mysql_data_seek($query, 0); Then the next call to fetch will be the first row again.

I haven't personally used it, but that's what I understood from the php manual: http://us.php.net/manual/en/function.mysql-data-seek.php

Upvotes: 1

Related Questions