Re: Setting session variables from a MySQL field value.
| From: | Mark Heintz PHP Mailing Lists | Date: | Wed, 15 Jan 2003 22:20:47 +0000 |
| Subject: | Re: Setting session variables from a MySQL field value. | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-131780@lists.php.net to get a copy of this message | ||
The problem may be your variable variable syntax... See if the following
works:
while ($row = mysql_fetch_array($result))
{
session_register('config_'.$row['config_id']);
${'config_'.$row['config_id']} = $row['config_value'];
}
Although I personally prefer using the session superglobal over
session_register, which means you'd just need a string for the key
value and can avoid variable variables altogether...
while ($row = mysql_fetch_array($result))
{
$_SESSION['config_'.$row['config_id']] = $row['config_value'];
}
mh.
On Wed, 15 Jan 2003, Stephen of Blank Canvas wrote:
> Hi Everyone,
>
> I'm trying to use MySQL to store some general settings for a site I am
> working on, which I was then going to access as session variables on
> pages as needed without having to call the dB each time. However I am
> having problems setting the name of the session variable in a way that I
> can reference it later. The code I am trying to use is:
>
> ===============================================================
> session_start();
> $connection =
> @mysql_connect("$MySQLHost","$MySQLUser","$MySQLPassword") or
> die("Unable to connect to MySQL Host. ".mysql_error() );
> $db = @mysql_select_db($MySQLdB, $connection) or die("Unable to
> connect to MySQL dB. ".mysql_error() );
> $query = "SELECT * FROM config WHERE status = 1";
> $result = @mysql_query($query,$connection) or die("Unable to execute
> select query. ".mysql_error() );
> $num_rows = mysql_numrows($result);
>
> while ($row = mysql_fetch_array($result))
> {
> $config_$row[config_id] = $row[config_value];
> session_register(config_$row[config_id]);
> }
>
> mysql_free_result($result);
> mysql_close($connection);
> ===============================================================
>
> Any pointers much appreciated.
>
> Stephen
>