我正在尝试使用while循环外的变量作为要检索的列名
<?php
include 'connection.php';
$user = "username";
$data = array();
$query = mysqli_query($conn, "select `username` from ewallet where `$user` = 'yes'");
while($result = mysqli_fetch_array($query))
{
$data = $result['username']." stat: ".$result['$user']."<br>";
}
echo $data;
?>
注意:未定义索引:$ user
答案 0 :(得分:0)
也许您可以执行以下操作:
<?php
include 'connection.php';
$user = "username";
$data = array();
$query = mysqli_query($conn, "select `username` from ewallet where `$user` = 'yes'");
while($result = mysqli_fetch_array($query))
{
$data[] = $result['username']." stat: ".$result[$user]."<br>";
}
var_dump($data);
?>
答案 1 :(得分:0)
您需要选择两列username
和$user
。然后,您不应在结果中引用$user
。
您应该将$data
初始化为字符串,然后使用.=
将每一行附加到该行,而不是每次循环都覆盖它。
<?php
include 'connection.php';
$user = "ian";
$data = "";
$query = mysqli_query($conn, "select `username`, `$user` from ewallet where `$user` = 'yes'");
while($result = mysqli_fetch_array($query))
{
$data .= $result['username']." stat: ".$result[$user]."<br>";
}
echo $data;
?>