...

View Full Version : extract() advice required



Taylor_1978
06-04-2004, 04:54 PM
Okay... I am just going to post the code I have used. It doesn't work but I am sure you will all see what I am trying to do - I assume I sort of have it backwards.



$result = mysql_db_query(ysl, "SELECT * FROM users WHERE id='$uid'");
$row = mysql_fetch_array($result);
$_SESSION["extract($row)"];
echo "$_SESSION[uid]";


No errors come up - but the session uid does not set.

Any help appreciated.

Thanks in advance! :thumbsup:

Taylor.

carl_mcdade
06-04-2004, 05:01 PM
Is session_start() at the top ofthe page?

Taylor_1978
06-04-2004, 05:07 PM
My session_start() does not need to be on - global_register is on. However thats not the issue. :) It's the $_SESSION[extract($row)]. Basically I am trying to save myself from doing this:



extract($row);
$_SESSION[username] = $username;
$_SESSION[email] = $email;
$_SESSION[yaps] = $yaps;
// etc etc etc etc


Taylor.

carl_mcdade
06-04-2004, 05:49 PM
Have you tried it without the quotes? or How about




<?php

$size = "large";
$mySQLdata = array("color" => "blue",
"size" => "medium",
"shape" => "sphere");


foreach ($mySQLdata as $a=>$b){
$_SESSION[$a] = $b;
}

foreach ($_SESSION as $c=>$d){
print $c . "=";
print($d) . "<br>";
}
?>

Taylor_1978
06-04-2004, 05:57 PM
Yep I have tried it without the quotes, and no the while() cannot be used because that would be asking for it to extract whenever id=uid - which is only once.

Taylor_1978
06-04-2004, 06:14 PM
Found the answer:



$result = mysql_db_query(ysl, "SELECT * FROM users WHERE id='$uid'");
$row = mysql_fetch_array($result);
function xtract($a,$prefix) {
foreach($a as $key=>$val) $_SESSION["$prefix_{$key}"]=$val;
}

xtract($row, "l");

echo "$_SESSION[l_username]";
echo "$_SESSION[l_email]";

carl_mcdade
06-04-2004, 06:27 PM
Yeah, you found that while I was stinking out my own code. If you check the above again you'll we came to the same solution ;)

firepages
06-05-2004, 03:11 AM
well you can simply either ...



<?
$_SESSION = array_merge( $_SESSION , $row ) ;
?>
or even
<?
$_SESSION += $row ;
?>


though the end solution you ended up with is probably better as it lets you decide whether to overwrite existing array keys and/or prefix them.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum