Question

This is my first web programming project for years so please forgive me if the question is really basic. Here's my code

<?php
require_once('base.php');

//connect to DB
$conn = mysql_connect($dbhost, $dbuser, 'wrong password') 
     or die ("Unable to establish connection to MySQL: " + mysql_error($conn));
?>

The base.php defines the variables $dbhost and $dbuser. As you can see, I intentionally used a wrong password for DB connection. When I open the page in a browser, I expect to see the text "Unable to establish connection to MySQL: ... ". However, what I see is:

enter image description here

When I change die() to echo(), nothing changes. What am I missing? I am using Wamp.

Était-ce utile?

La solution

You're using the wrong concatenation operator. + is for JavaScript. In PHP we use ..

or die ("Unable to establish connection to MySQL: " + mysql_error($conn));

should be:

or die ("Unable to establish connection to MySQL: " . mysql_error($conn));

edit

To follow up on @MichaelBerkowski's and @Jessica's comments, you should see nothing as your output from die() since PHP will type juggle those two strings, type juggle them into zeros, and give you a sum of zero. die(0) will then output nothing. This will change once you implement the solution mentioned above.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top