Pergunta

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;
 }
Foi útil?

Solução 2

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

Outras dicas

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top