Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can a MYSQL Resultset Be Passed as a PHP Parameter? 1

Status
Not open for further replies.

zombee

Programmer
May 16, 2008
18
0
0
US
Struggling to pass a resultset gotten via PHP code
$resultset = mysql_query($q);
as a parameter to the next php file,
to be read by something like
$parm = $_POST['resultset'];
to here begin fetching each row.

Is this a Mysql restriction or my ignorance of good PHP code?
If the latter, would appreciate your help.
 
This would be a PHP question not MYSQL.

However you can't do it that way, because the php msyql functions don't return a result set per say, but more of a pointer to a result set. If you need to store the results form a query, you can do it using an array and then pass it in a session variable.

Code:
$myvariable=array();

while($row=mysql_query($Q)){
$myvariable=$row;
}

session_start();
$_SESSION['results']=$row;


Then you can use the session var in your other page.

However I'm not sure why you would want to pass the entire result set to another page, perhaps passing just the query string generated and then running it in the other page would be more practical.


For any other PHP related question you'd be best served by posting in forum434.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Interesting question really. The Microsoft ADO model introduced the concept of disconnected record sets which is essentialy an XML file which contains rows of data and some meta data describing the data.
It allows you to have disconnect processing and also possibly more importanty in the web world reuce the hit on a database server by using the recordset as a cache and as long as you know if's not going to change (e.g. a list of postcodes/zip coded to county mapping or a list of order lines on a given order) all that often you can read it once and use the record set.
This answers vacunita's question about why you might want to do this. As to how you might get PHP to do it try a session variable, get it into the post stream (perhaps by a hidden field) or what might be a counter intuative move stored it back in the database and pass a referecne to it. You would only do this if the query took a long time to run and you would save real time and processing time by taking this step.
 
I posted this question a little differently in the PHP forum,
under the title "How do I access a Mysql resource from another PHP file?",
if you'd kindly take a look there.

I'd be interested to know if vacunita's proposed solution would suit me
(and if yes, to be a little more specific).
 
My code in m previous post is grossly wrong. However What i was proposing is if you need the results from a query done elsewhere, you need to store them somewhere and then redirect them. Or Store the query string you used to get those results and redirect it and then run it again.

Whatever benefit you would get by not having to re-rerun the query elsewhere, you loose by having to transfer large amounts of data amongst PHP pages.

Still it can be done.

To correct my previous post

Code:
session_start();
while ($row=mysql_fetch_array($res)){

$_SESSION['dbresults'][]=$row;

}

You query results will be available in the Session variable in your other page.

Alternatively you can store the querystring in the session variable instead.




----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
vacunita - Your solution doesn't quite fit my paradigm in that
the php file that does the query - does only that.
The NEXT php file processes this resultset record by record.

Can I overcome the php overhead you speak of by using
a reference to the resultset? A reference, after all,
does not copy the variable's contents as would an assignment.
 
I could have sworn I posted a response earlier today, o.k well here goes gain:

The page that runs the query needs to do more than that, otherwise you won't have access to the results.

"including" or "requiring" the page that runs the query may be an option but from your other posts in the PHP page seems like its not.

By including the script in another page it is run, and the results made available to the page that is calling it.

But if "including" is not an option, you are limited to storing the results somewhere, or re-running the query thats it.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Including" is a great idea.
But please tell me this:
Would an ajax call, using a second PHP script, cause the loss of the included variable's contents?
 
I'm not sure I understand the question.

The second PHP script would itself include the original script Which means the result set would be available to it.

However if you include the first script it will effectively include everything it does. That is if it displays anything originally it will display it again once it is included.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top