0

I am using mysqli to fetch data from the database and put it into an array with a while loop. When i echo out the array i get an empty array however in a function i previously did this code worked but it had a different result from the database. I know that the database is giving out good data because when i echo out the result $idGroup it gives me 2 which is correct.

Ps i know it will keep replacing itself because i don't specify an index private function Groups() { $functionRun = 0; $i = 0; $helperArray = array(); $this->grouplist = array();

  $query = "SELECT GroupName, Groups.idGroup  
            FROM Groups
            INNER JOIN Members
            ON Groups.idGroup = Members.idGroup
            WHERE Members.idMember = ? ";

  //prepare query and execute
  if($stmt = $this->connection->prepare($query))
  {

    $stmt->bind_param('s', $this->id);
    $stmt->execute();
    $stmt->bind_result($GroupName, $idGroup);

    while ($stmt->fetch()) 
    {

       $helperArray[] = $idGroup;

    }

        echo $helperArray;
 }
2
  • You should use print_r on $helperArray, not echo Commented Mar 3, 2014 at 23:50
  • wow easy fix should have known that thanks Commented Mar 3, 2014 at 23:51

2 Answers 2

2

Use print_r when dealing with arrays. Use echo on strings.

Sign up to request clarification or add additional context in comments.

Comments

1

Try this

 $query = "SELECT GroupName, Groups.idGroup  
        FROM Groups
        INNER JOIN Members
        ON Groups.idGroup = Members.idGroup
        WHERE Members.idMember = ? ";

//prepare query and execute
if($stmt = $this->connection->prepare($query))
{

$stmt->bind_param('s', $this->id);
$stmt->execute();
$stmt->bind_result($GroupName, $idGroup);
$helperArray =array();
while ($stmt->fetch()) 
{

   $helperArray[] = $idGroup;

}

    print_r($helperArray);
}

Comments

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.