Multiple values from a db into an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
eatspinach
Forum Newbie
Posts: 20
Joined: Mon Apr 20, 2009 8:50 am

Multiple values from a db into an array

Post 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?
Last edited by Benjamin on Mon Jun 08, 2009 10:28 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
Raph
Forum Commoner
Posts: 43
Joined: Wed May 27, 2009 6:33 pm

Re: Multiple values from a db into an array

Post by Raph »

When using var_dump($questionSet), what do you get when you browse the sourcecode?
eatspinach
Forum Newbie
Posts: 20
Joined: Mon Apr 20, 2009 8:50 am

Re: Multiple values from a db into an array

Post 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??
Post Reply