Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am having some trouble getting MySQLi to connect and respond properly. It should be relaying a connection error any time it is unable to connect to the database, but it doesn't, even when enter invalid details in an attempt to force a connection error.

$emailaddress = $_POST['emailaddress'];
$password = $_POST['password'];
if ($emailaddress&&$password)
    {
        $db = new mysqli('loalhost','rot','','FitessHouse');
        if($db->connect_errno > 0)  
        {
            die('Unable to connect to database [' . $db->connect_error . ']');
        }
    }

EDIT: Let me be more clear. I am trying to get MySQLi to return a connection error because the connection fails. I'm not trying to fix the connection. When I hit "Submit" it takes me to a blank page, which means my die statement is not working.

Fixed it and now have this error:

Notice: Undefined index: emailaddress in /Applications/XAMPP/xamppfiles/htdocs/FitnessHouse/login.php on line 5

share|improve this question
 
> it takes me to a blank page, which means my die statement is not working. Actually it means only that you see a blank page. And there could be 1000 reasons for that To be sure that your die statement is not working, you have to add an else condition, to prove that. though I am sure it won't be run too –  Your Common Sense May 9 at 14:28
 
 
Please paste the relevant code from pastebin to your question. –  FreshPrinceOfSO May 9 at 14:33
add comment

1 Answer

This line of code should display the error and stop the script:

$db = new mysqli('loalhost','rot','','FitessHouse')
    or die('Unable to connect to database [' . $db->connect_error . ']');

You can also use trigger_error instead of die:

$db = new mysqli('loalhost','rot','','FitessHouse');
if($db->connect_errno)
    trigger_error('Unable to connect to database [' . $db->connect_error . ']', E_USER_ERROR);
share|improve this answer
1  
please change die() to trigger_error() –  Your Common Sense May 9 at 14:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.