Forum Moderators: coopster

Message Too Old, No Replies

session data not being saved with mysql_fetch_array

works with mysql_fetch_assoc

         

ergophobe

8:27 am on Dec 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Subject pretty much sums it up. If I do the following, everything works great

if ($user_info = mysql_fetch_assoc($login_result)) {
foreach($user_info as $key => $val) {
$$key = stripslashes($val);
$_SESSION[$key] = $val;
}
}

however, if instead I use
if ($user_info = mysql_fetch_array($login_result)) {rest is the same}

it saves nothing to the session file and crashes when I call a session var "index does not exist". Sure enough if I print_r($_SESSION), I get nothing but an empty array.

I thought it might be because $$key doesn't give a proper name for the index when it is the numeric index, but when I do a print_r($_SESSION) before going to the next page, everything looks as it should
Array ( [0] => Baggins [last_name] => Baggins [1] => Bilbo [first_name] => Bilbo)

Why is that?

Tom

bobnew32

11:24 am on Dec 18, 2003 (gmt 0)

10+ Year Member



$$key = stripslashes($val);

Too many $ signs to begin with

ergophobe

4:14 pm on Dec 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




Too many $ signs to begin with

Can you elaborate? I think it's correct as is. As I said, it works as expected with fetch_assoc, just doesn't work with fetch_array.

Tom

ergophobe

4:17 pm on Dec 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



PS Bobnew,

Perhaps you're being tripped up by the variable variable. The idea is not to assign $val to $key, but to assign $val to a variable whose name is equal to the value of $key.

so

$key = 'x';
$val = 321;
$$key = $val; // thus $x = 321

That should be simple enough and I don't think there's any problem there. Although variable variable can get turned around in my head.

Tom

jatar_k

4:53 pm on Dec 18, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



It has to have something to do with your logic and the double indices.

if ($user_info = mysql_fetch_array($login_result, MYSQL_ASSOC)) {

I would think it would work like so, why do you want double vars set in the session?

It gives you the error on the next page? I would try something like

<?
session_start();
echo "<pre>";
print_r($_SESSION);
echo "</pre>";
?>

just to look at what exactly ends up in there on the next page.

ergophobe

5:18 pm on Dec 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



EDIT:

OK, this is rather embarassing. Now I can't get it to NOT work. Last night I opened new browsers to make sure it wasn't a simple cache problem, always with the same result. Now that's all I can figure. I wish I had archived those files that weren't working.

Sorry for wasting everyone's time.

[sutpid stuff deleted]

Tom

jatar_k

7:34 pm on Dec 18, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



oh well, it astounds me the amount of times we all have this happen. No real surprise that it was with sessions though. I often have them do very strange things. :)