PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, April 21, 2022

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

 April 21, 2022     conditional-statements, connection, mysql, php     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing