我的建议是使用 mysqli 功能:
<?php
$mysqliClass = mysqli_init();
$mysqliClass->real_connect("hostname or IP", "dbUsername", "dbPassword", "databasename");
$query = "SELECT `username` AS display_name, `fullname` AS full_name, `email`, `role` as administrators, `status` as be_on FROM `admin`";
$rs = $mysqliClass->query($query); # Preform your query
$showUser = false; # Start negative
$row = []; # Set a variable for the row to go into
while ($result = mysqli_fetch_assoc($rs)) # Fetch each result one at a time
{
if ($result['administrators'] == 1) # Check the role
{
$showUser = true; # Set the show to true if it equals 1
$row = $result; # Set the row to equal the result given
}
break; # You only need the first result
}
if ($showUser)
{
/* Do your work with $row! */
}
?>
我还删除了 email AS email 的 email,因为它已经有了这个名称,所以它是不必要的混乱。
我还建议在您的查询中添加 WHERE 子句以减少(过滤)结果
如果您使用数据库中的图像,您可以执行以下操作:
<img src="<?=$row['image_column_name'];?>" height="120" width="120" />
而这个应该按照你想要的方式显示图像:)
Source of example code
更完整的示例,如原始答案:
<?php
$mysqliClass = mysqli_init();
$mysqliClass->real_connect("hostname or IP", "dbUsername", "dbPassword", "databasename");
$query = "SELECT `name`, `logo` AS describe , IF(`status` = 0, 'Online', 'Offline') as be_on FROM `domain`";
$rs = $mysqliClass->query($query);
$rows = []; # This will have more than one in it this time!
while ($result = mysqli_fetch_assoc($rs))
{
$rows[] = $result;
}
foreach ($rows as $r)
{
foreach ($r as $rowKey => $rowValue) # Double loop because you have an array in an array this way
{
if ($rowKey == "describe")
{
print '<img src="' . $rowValue . '" height="120" width="120" />';
}
}
}
?>