0

I have got this code:

function retrieve_answers($array = array(), $id = null)
{
    include(root_path . '\config.php');
    if($id == null)
    {
        $id = $this->question_id;
    }
    $query = mysqli_query($link, "SELECT * FROM `answers` WHERE `question_id`='$id'");
    if(!mysqli_num_rows($query))
    {
        throw new Exception('Question not found.');
    }
    /* - Retrieves the answer rows
    - Loops through the array
    - Indexes the array and assigns the answerID to the index */
    else
    {
        while($result = mysqli_fetch_array($query))
        {
            for($i=0;$i<mysqli_num_rows($query);$i++) 
            {
                $array[$i] = $result["id"]; 
            }
        }
    }
}

Which is a part of a class.

What am I trying to do? I am trying to accept an array as a parameter, and assign values to the array, the values are to be answerIDs which are linked to the question.

The test.php file is here:

<?php
define('root_path', realpath(dirname(__FILE__)));
include(root_path . '\config.php');
require_once(root_path . '\includes\question.class.php');

$q = new Question(3);

$array = array();

$q->retrieve_answers($array);

var_dump($array);
?>

What happens? When I try to debug by dumping the array, it shows that the array contains nothing:

array(0) { }

I tried to execute the MySQL result through the class to debug, and it does succeed to retrieve the answer IDs, so I'm pretty positive the issue is in the array.

I would happy to get assistance, thanks in advance.

1
  • just return that array and assign it during the function execution Commented Jun 2, 2015 at 11:09

1 Answer 1

1

return value in a function like this

function retrieve_answers($array = array(), $id = null)
{
    include(root_path . '\config.php');
    if($id == null)
    {
        $id = $this->question_id;
    }
    $query = mysqli_query($link, "SELECT * FROM `answers` WHERE `question_id`='$id'");
    if(!mysqli_num_rows($query))
    {
        throw new Exception('Question not found.');
    }
    /* - Retrieves the answer rows
    - Loops through the array
    - Indexes the array and assigns the answerID to the index */
    else
    {
        $i = 0;
        while($result = mysqli_fetch_array($query))
        {
            $array[$i] = $result["id"];                 
            $i++;
        }
    return $array;
    }


}

and then get it as

$arr = $q->retrieve_answers($array);

var_dump($arr);
Sign up to request clarification or add additional context in comments.

7 Comments

I don't have 15 reputation so I can't, sorry! I accepted it though.
I have encountered another issue, I've added another answer to link to the same question, and the answer IDs that are executed via var_dump are the same, aka: 2,2 instead of 1,2 array(2) { [0]=> string(1) "2" [1]=> string(1) "2" }
Can you please explain that
I've added another answer which is linked to the question, basically it looks like this in the MySQL table: Table: id question_id answer_text Data: 1 3 Blabla 2 3 Blabla And instead of showing 1,2 as a result of the array, it shows 2,2 -> a.k.a the last result of the mysqli_fetch_array.
Thanks :) I did $array[] = $result["id"]; and it indexed automatically, works now.
|

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.