我正在尝试从数据库中获取列并将整个内容显示为数组。到目前为止,我只能从表中获取其中一个条目。我知道我只获取$ row [0]这就是为什么我只得到1个元素。我试图获取所有元素,但我还没有成功。有人可以让我知道如何正确地做到这一点?我在下面附上了我的代码。
<?php
$con=mysqli_connect("localhost","root","raspberry","users");
if (mysqli_connect_errno())
{
echo "failled to connect:".mysqli_connect_error();
}
$sql = "SELECT `current` FROM `monitor` ORDER BY `Sno.`";
$result = mysqli_query($con,$sql);
$row = mysqli_fetch_array($result,MYSQLI_NUM);
printf("%s\n",$row[0]);
mysqli_close($con)
?>
答案 0 :(得分:0)
PDO为您的问题PDOStatement :: fetchColumn提供专用功能。
答案 1 :(得分:-1)
所以,从你的问题我理解的是 - 你想获取一个数据库列并将其存储在一个数组中,这将使你显示结果。
<?php
$con=mysqli_connect("localhost","root","raspberry","users");
if (mysqli_connect_errno())
{
echo "failled to connect:".mysqli_connect_error();
}
$sql = "SELECT current FROM monitor ORDER BY Sno";
$result = mysqli_query($con,$sql);
$arr = array();
while ($row = mysqli_fetch_array($result)) {
$arr2 = array();
foreach ($row as $val) $arr2[] = $val;
$arr[] = $arr2;
}
$first_col = array_column($arr, 0);
print_r($first_col);
mysqli_close($con)
&GT;
即。如果您有第一列包含的数据库的五个条目 像'current1','current2','current3','current3'和'current4'这样的值将是
的结果阵 ( [0] =&gt;当前第 [1] =&gt;当前第 [2] =&gt; current3 [3] =&gt; current4 [4] =&gt; current5 )