【问题标题】:Fetching data from database into array从数据库中获取数据到数组中
【发布时间】:2014-04-05 06:48:25
【问题描述】:

我正在使用 mysqli 从数据库中获取数据并将其放入带有 while 循环的数组中。当我回显数组时,我得到一个空数组,但是在一个函数中,我之前确实执行过此代码,但它与数据库的结果不同。我知道数据库提供了良好的数据,因为当我回显结果 $idGroup 它给我 2 这是正确的。

Ps 我知道它会不断替换自己,因为我没有指定索引 私有函数组() { $函数运行 = 0; $i = 0; $helperArray = 数组(); $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;
 }

【问题讨论】:

  • 你应该在 $helperArray 上使用 print_r,而不是 echo
  • wow easy fix 应该知道谢谢

标签: php mysqli


【解决方案1】:

在处理数组时使用 print_r。在字符串上使用 echo。

【讨论】:

    【解决方案2】:

    试试这个

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-23
      • 1970-01-01
      • 1970-01-01
      • 2013-04-25
      • 2019-12-20
      • 2014-09-24
      • 1970-01-01
      相关资源
      最近更新 更多