简体   繁体   English

从数据库中获取数据到数组

[英]Fetching data from database into array

I am using mysqli to fetch data from the database and put it into an array with a while loop. 我正在使用mysqli从数据库中获取数据,并将其放入带有while循环的数组中。 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. 我知道数据库发出了很好的数据,因为当我回显结果$ idGroup时,它给了我2个正确的信息。

Ps i know it will keep replacing itself because i don't specify an index private function Groups() { $functionRun = 0; 附言:我知道它将不断替换自身,因为我没有指定索引私有函数Groups(){$ functionRun = 0; $i = 0; $ i = 0; $helperArray = array(); $ helperArray = array(); $this->grouplist = 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;
 }

Use print_r when dealing with arrays. 处理数组时使用print_r。 Use echo on strings. 在字符串上使用echo。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM