Reputation: 4359
I'm trying to make sessions based off sql query data is being returned
this is my code:
$r = self::$db->fetch_row_assoc($sql);
$theme_dir = THEME_PATH . self::get_useragent() . "_UA_" . $flag . "_" . $r['theme_folder_name'];
if (self::$config['set_session'] == 1) {
foreach ($r as $k => $v) {
self::$session->set(self::get_useragent() . "_UA_" . $flag . "_" . $k, $v);
}
self::$session->set(self::get_useragent() . "_UA_" . $flag . "_theme_loaded", 1);
}
when this code is run the makes my sessions but i get something like
$_SESSION['Default_UA_landing_theme_id'] = 1
$_SESSION['Default_UA_landing_0'] = 1
it does this for every session that it creates, it makes 1 session with $k being the field name of the result returned by my query then it makes another with $k being the index of the $r array.
how can I just have it make one session per $k?
this is the method I use to query my datase
public function fetch_row_assoc($statement)
{
self::$PDO->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try{
$stmt = self::$PDO->query($statement);
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$result = $stmt->fetch(PDO::FETCH_BOTH); //FETCH_BOTH
return $result;
}catch(PDOException $e){
echo $e->getMessage();
}
return false;
}
Upvotes: 0
Views: 186
Reputation: 191729
You are using FETCH_BOTH
(creates an array with both numeric and associative indexes) and then cycling through a foreach
. Why are you using FETCH_BOTH
immediately after specifying FETCH_ASSOC
?
Upvotes: 1
Reputation: 81988
Without knowing more about what's going on in with fetch_row_assoc
, I can't really be certain, but it looks like you've got numeric and non-numeric indexes in what should be an associative array. You should probably look at whatever class is behind self::$db
and see if there isn't another method which could fix that.
If the library you're working with won't give you an associative array without numeric indexes, then you could solve the above issue with a test for is_numeric
.
foreach ($r as $k => $v) {
if( is_numeric( $k ) )
self::$session->set(
self::get_useragent() . "_UA_" . $flag . "_" . $k,
$v);
}
Upvotes: 2