简体   繁体   English

当数据库连接/未连接时,如何使用MySQL / PHP显示不同的图像?

[英]How do I display different images using MySQL/PHP when database connected/not connected?

I'm new to PHP and MYSQL, and I have what I think is going to be a relatively easy question. 我是PHP和MYSQL的新手,我认为这是一个相对简单的问题。

My objective is to show one image (a green flashing light) when the database is connected, and display another image (a red flashing light) when there is no database connection. 我的目标是在连接数据库时显示一个图像(绿色闪烁的灯光),并在没有数据库连接时显示另一个图像(红色闪烁的灯光)。

I imagine it should be a simple variation on this: 我想这应该是一个简单的变化:

<?php
$servername = "localhost";
$username = "root";
$password = "";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";
?>

But if I attempt to add an image to where it echos "Connected successfully" I receive an error. 但是,如果我尝试将图像添加到回声“已成功连接”的位置,则会收到错误消息。

I'm attempting to add the image like this: 我试图像这样添加图像:

<?php
$servername = "localhost";
$username = "root";
$password = "";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
die("<img src="Red_Light.gif" style="width:10px;height:10px;"> " . $conn->connect_error);
} 
echo "<img src="Green_Light.gif" style="width:10px;height:10px;">";
?>

I probably have the completely wrong syntax but any help is appreciated. 我可能有完全错误的语法,但任何帮助表示赞赏。

Many thanks, Leif 非常感谢,Leif

You can use Janno answer or you may use this (by changing " to ' :- 您可以使用Janno答案,或者您可以使用此(通过更改" to ' : -

if ($conn->connect_error) {
die("<img src='Red_Light.gif' style='width:10px;height:10px;'> " . $conn->connect_error);
} 
echo "<img src='Green_Light.gif' style='width:10px;height:10px;'>";
if ($conn->connect_error) {
    die("<img src=\"Red_Light.gif\" style=\"width:10px;height:10px;\"> " . $conn->connect_error);
} 
echo "<img src=\"Green_Light.gif\" style=\"width:10px;height:10px;\">";

Whole problem seems to be related to not escaping the quote marks. 整个问题似乎与不逃避引号有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM