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

时间:2017-04-21 07:50:45

标签: php mysql

我目前正在处理我工作的状态网站。其目的是以服务名称旁边的刻度或交叉的形式显示正在运行或未运行的系统和服务。

我有一个包含服务名称的数据库,以及A或B的值.A应该导致勾号,B应该导致交叉。

我已经将代码放在一起检索A或B,并将其显示在相应名称旁边的表格中,但我想要改为勾选或交叉显示。

刻度线位于&sources / tick.png',十字架位于&sources / 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>

杰克

1 个答案:

答案 0 :(得分:1)

  

首先,对于所有SELECT, UPDATE, DELETE,您应该真的考虑使用PPS : Prepared Parameterized Statements。这将有助于Preventing SQL injection

然后,对于您的问题,如评论中所示:如果您的表包含状态设置为A / B(关/开)的wifi / Internet列,则只能进行一次查询,然后检查结果中每列的值循环 - &gt;如果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 ]"; }
?>