0

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;
    }

2 Answers 2

2

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

It seems the behavior of PDOStatemente::fetch(), that fetches every column 2 times, both with the column order and the column name. @s2xi investigate inside self::$db and see if you can change that (or pass $fetch_style = PDO::FETCH_ASSOC as parameter. (edit: changed mysql to pdo)
1

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?

2 Comments

well, i did make that method while researching how PDO handles queries. What would you suggest I fix it to?
its ok, i just set both to ASSOC and I got my results I was looking for

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.