Page 1 of 1

Multiple values from a db into an array

Posted: Mon Jun 08, 2009 5:02 am
by eatspinach
Hi

I am trying to query a database of questions to bring back the list of questions..

my code so far is

Code: Select all

 
<?php
 
$questionSet = getQuesSet($surveyId);
 
 
function getQuesSet($surveyId)
{
    $questions = func_get_args();
    
    $mysqli = db_connect();
        $query = "SELECT question FROM questions where surveyId = $surveyId";
    
    $target = 0;
    if ($result = $mysqli->query($query)) {
        $count = 0;
        /* fetch associative array */
        while ($row = $result->fetch_assoc()) {
            $questions[$count]  = $row["question"];
            $count++;
        }
 
        /* free result set */
        $result->close();
    }
 
    /* close connection */
    $mysqli->close();
    
    return $questions;
}
 
?>
 
I tried to return the values by putting this in the code, echo $questionSet[2];

but i get this error, Undefined offset: 2 ??

Does anyone have a better way of doing this?

Re: Multiple values from a db into an array

Posted: Mon Jun 08, 2009 6:56 am
by Raph
When using var_dump($questionSet), what do you get when you browse the sourcecode?

Re: Multiple values from a db into an array

Posted: Mon Jun 08, 2009 7:31 am
by eatspinach
Hi

Thanks for your reply, I get the following,

array
0 => string '18' (length=2)

18 is the survey id, which i don't even know how it got in there??