php - Retrieve data from a certain column and i can change the image whenever the data is different -
is possible retrieve data column , have if else statement can change image? want pic tally location retrieve database.
<?php $servername = "......byethost5.com"; $username = "...."; $password = "..."; $dbname = "b..."; // create connection $conn = new mysqli($servername, $username, $password, $dbname); // check connection if ($conn->connect_error) { die("connection failed: " . $conn->connect_error); } $sql = "select location,datetime sensordetails limit 1"; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { echo "last seen location: " . $row["location"]."<br>"; $help= $row["location"]; if($help=='toilet') {echo '<center><img src="toilet.jpg"></center>';} elseif($help=='kitchen') {echo '<center><img src="kitchen.jpg</center>';} else {echo '<center><img src="bedroom.jpg"></center>';} } } else { echo "0 results"; } $conn->close(); ?>
another solution use same name 'location' field , current image.
<?php $servername = "......byethost5.com"; $username = "...."; $password = "..."; $dbname = "b..."; ////////////////////////////////////////////////////// //image folder //put here folder save images $img_folder = './'; //current folder ///////////////////////////////////////////////////// // create connection $conn = new mysqli($servername, $username, $password, $dbname); // check connection if ($conn->connect_error) { die("connection failed: " . $conn->connect_error); } $sql = "select location,datetime sensordetails limit 1"; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { $img_file = $img_folder . $row["location"] . '.jpg'; //echo $img_file; //uncomment if wanna check img's path. if( ! file_exists($img_file) ){//check if file exists $img_file = ''; } echo "last seen location: " . $row["location"]."<br>"; echo "<center><img src="$img_file"></center>"; } }else { echo "<p> 0 results </p>"; } $conn->close(); ?>
this solution more appropriate if you're going have more locations in future. supposed keep images in same folder script , images in jpg format.
Comments
Post a Comment