首页 > 解决方案 > 如何在 $row[] 中使用变量的值;在 PHP 中?

问题描述

我不能在 $row[]; 中使用变量;

$a = id

然后当我这样做时:

$sql = "SELECT * FROM articles WHERE ID='$id'";

$result5 = mysqli_query($link, $sql);

if (mysqli_num_rows($result5) > 0)
{
while($row = mysqli_fetch_assoc($result5))
{
$aa = $row[$a];
  }

}

然后当我

echo $aa

我没有得到预期的结果。然后 $aa 应该返回像 193 这样的值,因为那是 id 列中的值。这是

 $aa = $row[$a]; 

似乎不起作用的部分。

标签: phpsql

解决方案


使用array_push(). 在此处查看文档

array_push($row, $a)

在您的代码中应用它:

$sql = "SELECT * FROM articles WHERE ID='".$id."'";

$result5 = mysqli_query($link, $sql);

if (mysqli_num_rows($result5) > 0)
{
    while($row = mysqli_fetch_assoc($result5))
    {
        $aa = $row[];
        array_push($aa, $a)
    }
    print_r($aa);

}

推荐阅读