php - 根据 MySQL 数据库中的值显示图像

标签 php mysql

我目前正在为我工​​作的地方开发一个状态网站。其目的是以服务名称旁边的勾号或叉号的形式显示哪些系统和服务正在运行或未运行。

我有一个数据库,其中包含服务名称以及 A 或 B 值。A 应产生勾号,B 应产生叉号。

我已经编写了代码来检索 A 或 B,并将其显示在相应名称旁边的表格中,但我希望显示勾号或叉号。

刻度线位于“sources/tick.png”,十字线位于“sources/cross.png”。

有人可以给我一些关于如何实现这一目标的指示吗?多谢!

下面是我的代码(我知道它很乱,但我仍在学习):

<?php
$mysqli = new mysqli("localhost", "root", "password", "status");
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo  '<img width="1%" src="sources/tick.png">' . "\n";

$sql1 = "SELECT wifi from status"; 
if(!$result1 = $mysqli->query($sql1)){
    die('There was an error running the query [' . $db->error . ']');
}

$sql2 = "SELECT internet from status"; 
if(!$result2 = $mysqli->query($sql2)){
    die('There was an error running the query [' . $db->error . ']');
}

?>

<html>
<head>
    <title>Network Status</title>
    <link rel="stylesheet" href="sources/styles.css">
</head>
<body>
<div id="header" align="center">
    <img class="headerImg" src="sources/logo.png"><br />
    <font class="headerTxt">The Current Network Status:</font>
</div>

<div id="wrapper" align="center">
    <table align="center">
        <tr>
            <td align="center"><span class="statusItm" id="wifi"><font class="statusTxt">WiFi</font></span></td>
            <td align="center"><span id="wifi"><?php while($row = $result1->fetch_assoc()){echo $row['wifi'] . '<br />';} ?></span></td> 
        </tr>
        <tr>
            <td align="center"><span class="statusItm" id="internet"><font class="statusTxt">Internet Access</font></span></td>
            <td align="center"><span id="internet"><?php while($row = $result2->fetch_assoc()){echo $row['internet'] . '<br />';} ?></span></td> 
        </tr>
    </table>
</div>
</body>
</html>

jack

最佳答案

1st of all, for all SELECT, UPDATE, DELETE, you should really consider using PPS : Prepared Parameterized Statements. This will help Preventing SQL injection

然后,对于您的问题,如评论中所示:如果您的表包含状态设置为 A/B(关闭/打开)的 wifi/internet 列,您只能进行一个查询,然后检查结果中每一列的值循环 -> 如果 wifi==A(打开)则显示勾号,否则显示十字,互联网也是如此。因此,在您的 div 中,不要显示 $row['internet'] 使用 img 标签

您需要适应的示例:

<?php

error_reporting(E_ALL); ini_set('display_errors', 1); /* PHP will help us */

/* connexion to db */
$mysqli = mysqli_connect("$host", "$user", "$pwd", "$db");
/* adapt to your credentials */

if (mysqli_connect_errno()) { echo "Error: no connexion allowed : " . mysqli_connect_error($mysqli); }

$query = " SELECT `internet`, `wifi` FROM `status` ";

$stmt = $mysqli->prepare($query); /* prepare query */

$results = $stmt->execute(); /* execute query */
$stmt->bind_result($internet, $wifi); /* get results */
$stmt->store_result();

// print_r($stmt->error_list); /* check for error */
// print_r($stmt->get_warnings()); /* check for error */
// print_r($stmt->error); /* check for error */

if ($stmt->num_rows > 0) { /* we have results */
/* here, you may want to start CSS table -> end php -> write raw html -> back to php */

while($stmt->fetch()){ /* loop through results */

/* here you add your CSS table tr / td for the results */

if($wifi == "A") { echo"<img src=\"sources/tick.png\" alt=\"\" />"; } else { echo"<img src=\"sources/cross.png\" alt=\"\" />"; }
if($internet == "A") { echo"<img src=\"sources/tick.png\" alt=\"\" />"; } else { echo"<img src=\"sources/cross.png\" alt=\"\" />"; }
}

/* here, you close CSS table -> end php -> write raw html -> back to php */

}
else
{ echo"[ no data ]"; }
?>  

关于php - 根据 MySQL 数据库中的值显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43536940/

相关文章:

php - MySQL 时间戳转 PHP 日期时间

java - JAVA 错误 1070 中的 PIG UDF

MYSQL less Than函数不能正常工作

javascript - 文本区域和选择输入的数据或值保留

javascript - 用于过滤的 PHP 和 jquery Ajax 和无限滚动

php - 如何在PHPMailer中添加文件附件?

mysql - 我如何查询拆分为 2 个较小表的表?联盟?看法?

php - 请求不返回表单中的所有数据

php - 为 PHP-FPM 使用 HOSTALIASES

mysql - 如何用字符串包装(前缀和后缀)列中的所有值?