2

Newish to php. I have been trying to query a database, and I keep getting the exception thrown that the query could not be completed. I checked to make sure I was connecting to the database, and everything looked fine, until I dug deeper. It appears that the my code tells me that I am connecting to the database regardless of what I put in for a password, username, or even if I do not have this data defined. I don't get it. Originally I had the following code in a function, but I put it no its own page to debug:

<?php
echo'this is working so far <br>';

/*$db = 'fake';
$host = 'localhost';
$password = 'wrong';
$user = 'root';
*/

$result = new mysqli($host, $user, $password, $db);
if(!$result){
     echo 'did not connect to database';
    throw new Exception('Could not connect to database');

}
else{
    echo'connected to database';
    return $result; 
}

It always tells me I am connected to the database..

Saty
  • 22,443
  • 7
  • 33
  • 51
ratrace123
  • 976
  • 4
  • 12
  • 24
  • Why are your database's variables ($host, $password and $user) commented?, if you are executing the same code as above make sure to un-comment them. – Shaher Jamal Eddin Jan 07 '16 at 07:40
  • i commented them as a sanity check to make sure that no matter what i did, the script was still telling me i was connected to the database.. – ratrace123 Jan 07 '16 at 08:24

3 Answers3

2

Because you are mixing Object oriented style with Procedural style To check database connection

Procedural style

<?php
$link = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');

if (!$link) {
    die('Connect Error (' . mysqli_connect_errno() . ') '
            . mysqli_connect_error());
}

echo 'Success... ' . mysqli_get_host_info($link) . "\n";

mysqli_close($link);
?>

Object oriented style

<?php
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
if ($mysqli->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') '
            . $mysqli->connect_error);
}
?>

Read http://php.net/manual/en/mysqli.construct.php

Saty
  • 22,443
  • 7
  • 33
  • 51
  • 1
    ahh i see, thanks for the fast response. I am following this book which is really good [link](http://www.amazon.com/PHP-MySQL-Web-Development-Edition/dp/0672329166) and unless i made a mistake, i think I am doing it the same way that they are. So for the OO style, the 'new mysqli()' function creates a new object called $mysqli, whereas the procedural style creates a resource $link? – ratrace123 Jan 07 '16 at 08:22
  • 1
    @Saty great answer. But I have a question about the usage of both styles. What if I am for example connecting to the database with the OO style and preforming the query in Procedural style. Would it still give out an error? I know it is bad practice because in the long run you don't know what you used before and it is overall just bad practice but I was wondering. – BRoebie Jan 07 '16 at 09:07
  • 1
    @BRoebie please read [this](http://stackoverflow.com/questions/16756002/is-it-acceptable-to-use-a-mix-of-object-oriented-style-with-procedural-style-in) – Saty Jan 07 '16 at 09:49
  • Thank you this is exactly what I am looking for – BRoebie Jan 07 '16 at 10:06
  • Happy to help!!@BRoebie – Saty Jan 07 '16 at 10:08
1

You should check connect_errno property which stores the error code from last connect call.

$mysqli = new mysqli($host, $user, $password, $db);
/* check connection */
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}
Alexander
  • 12,424
  • 5
  • 59
  • 76
1

Note:

OO syntax only: If a connection fails an object is still returned. To check if the connection failed then use either the mysqli_connect_error() function or the mysqli->connect_error property as in the preceding examples.

Source

That means if($result) check is always true no matter what. So no, you don't have that database connection but you are verifying it incorrectly leading you to believe you do.

Your check should be

if($result->connect_error)
     // no luck
else
     // game on
Community
  • 1
  • 1
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95