Thursday, April 21, 2022

[FIXED] How do I check if I'm connected to database?

Issue

This is the code to connect to the mysql database:

$con = mysql_connect("", "", "");

if (!$con) {
  die('Could not connect: ' . mysql_error());
}

mysql_select_db("", $con);

I want to echo 'connected'/'disconnected' depending on state.

How can it be done?


Solution

Do it Like this

if ($con) {
  echo 'connected';
} else {
  echo 'not connected';
}

Or try this

echo $con ? 'connected' : 'not connected';


Answered By - Danil Speransky
Answer Checked By - David Goodson (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.