Jump to content

Using mysql_fetch_array and sessions


phpknight

Recommended Posts

I am running into a weird issue.  Normally, if you store the result from a query in a variable you can go through it with mysql_fetch_array.  I am attempting to store the result in a session variable, but when I try to run through it using ajax and mysql_fetch_array, it just does not work as expected. 

 

Should you be able to use mysql_fetch_array with database results with persistent results in a session variable? I am not sure if I am doing something wrong or just need to make the application a different way.

Link to comment
Share on other sites

I'm not sure what your asking here?! Does your code look like one of these?

 

<?php
$sql = "Some Query";
$result = mysql_query($sql);

// this
$session['result'] = $result;
// or this
$session['result'] = mysql_fetch_array($result);
?>

Link to comment
Share on other sites

The problem with this is that once the script stops executing the mysql connection is closed and those mysql resources will no longer be valid.

 

Explain your situation better and we can help you find an alternative.

Link to comment
Share on other sites

I just figured that out myself.  I guess I posted here because I was thinking it had something to do with variable types. 

 

So, I guess the other way would be to get the results once, copy them to an array row by row, and then store the array to a session variable?  Alternately, I could query just one row each time.  Any better ideas?

 

 

Link to comment
Share on other sites

I don't know your specific situation so it's hard to give you advice. You could be going in a completely wrong way and I wouldn't know because I don't know what you're trying to accomplish.

 

From the given information it seems like you'd be best off doing something like:

 

session_start();
//Query stuff
$_SESSION['result'] = Array();
while($row = mysql_fetch_assoc($result))
     $_SESSION['result'][] = $row;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.