1

I'm new to PHP and MYSQL, and I have what I think is going to be a relatively easy question.

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

2 Answers 2

2

You can use Janno answer or you may use this (by changing " 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;'>";
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, single-quotes between doublequotes or vice versa works too. Could've added this as a comment instead of a separate answer tho.
@Janno hehe, kinda lazy to write as comment, so I write it as answer, let him/her choose :D
1
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.

1 Comment

Thank you for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.