-2

I'm trying to echo out "Connected" or Disconnected to see if im connected to my database or not.

This is my code:

$con = mysqli_connect("localhost", "root", "") 

mysqli_select_db($con,"online_ticket");

What if statements will be possible to test my connection?


Any help is much appreciated.

Anubis
  • 1
  • 1
  • 4
  • You just had to google for **mysqli_connect** and found the answer is what @spencer7593 answered. – outman Apr 29 '18 at 23:32
  • 1
    Note: The object-oriented interface to `mysqli` is significantly less verbose, making code easier to read and audit, and is not easily confused with the obsolete `mysql_query` interface. Before you get too invested in the procedural style it’s worth switching over. Example: `$db = new mysqli(…)` and `$db->prepare("…")` The procedural interface is an artifact from the PHP 4 era when `mysqli` API was introduced and should not be used in new code. – tadman Apr 30 '18 at 00:09

2 Answers2

0

mysqli_connect will return FALSE if a connection is not successful; it will return a connection handle if it is successful.

http://php.net/manual/en/function.mysqli-connect.php

$con = mysqli_connect("localhost", "root", "");
if (!$con) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}
spencer7593
  • 106,611
  • 15
  • 112
  • 140
-2
$conn = mysqli_connect("127.0.0.1","username", 
                 "password", "db");
if (!$conn) {
    die(mysqli_connect_error());
}
echo "<p>Connected!</p>";
blackers
  • 61
  • 7