【问题标题】:Storing db value into php array with while loop?使用while循环将db值存储到php数组中?
【发布时间】:2013-08-19 06:39:21
【问题描述】:

如果 Unit = Child,我想搜索 AdvInfo 表并存储主键。

 $result = mysqli_query($conn, "SELECT * FROM AdvInfo WHERE Unit = Child");     
 while ($row = mysqli_fetch_array($result, SQLSRV_FETCH_ASSOC))
 {

$childhbc = array_merge($childhbc, $row[0]);
echo $childhbc[0];
 }

【问题讨论】:

  • 你能回应你的选择查询吗?
  • 没有。回显 $childhbc 也不会返回任何内容

标签: php mysql arrays loops mysqli


【解决方案1】:

array_merge 的第二个参数应该是一个数组。但是引用文档:

mysqli_fetch_array [...] 返回对应的字符串数组 到获取的行,如果结果集中没有更多行,则为 NULL。

在您的代码中,$row 是一个数组$row[0] 是一个字符串

while ($row = mysqli_fetch_array($result, SQLSRV_FETCH_ASSOC))
{
    $childhbc = array_merge($childhbc, $row);
    echo $childhbc[0];
}

【讨论】:

    【解决方案2】:

    保持简单,伙计

    $childhbc = array();
    $result = mysqli_query($conn, "SELECT * FROM AdvInfo WHERE Unit = Child");     
    while ($row = mysqli_fetch_row($result))
    {
        $childhbc[] = $row[0];
    }
    

    您使用的词越不复杂 - 您的程序运行得越好

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-15
      • 2016-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-20
      • 1970-01-01
      相关资源
      最近更新 更多