0

I am attempting to connect to a mySQL database running on my local computer using a local test server set up using XAMPP. I am just using the root account which has no password assigned. I tried it as below and everything appears to work fine. I got my message echoed out to say I had successfully connected. I then tried to force an error by entering anything else in as the host, username or password and it still echoes "connection successful".

This is the code I am using to connect:

<?php
$db_host = "localhost";
$db_username = "root";
$db_password = "";

$myConn = mysql_connect($db_host, $db_username, $db_password) or die('Connection error: '.mysql_error());
echo 'connection successful';
?>

I was wondering whether it is me or maybe the install of apache/mySQL that I am using that is causing such an absurdity.

3 Answers3

0

Something must be screwed up with your install:

marc@panic:~$ php -a
php > $x = mysql_connect('bleargh', 'foo', 'bar') or die(mysql_error());
PHP Warning:  mysql_connect(): Unknown MySQL server host 'bleargh' (2) in php shell code on line 1
Unknown MySQL server host 'bleargh' (2)marc@panic:~$ 

About all I can think of is you've got a DNS server that returns a valid address for ALL lookups, causing all hostnames to resolve.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • If error_level is `E_ERROR` warnings aren't displayed, and also `display_errors` could be off. – Burhan Khalid Aug 23 '12 at 05:18
  • doesn't matter. mysql_connect() would still return a false on error, even if the error messages aren't being displayed, which should trigger the `or die()`. PHP may not be the greatest, but even PHP will honor errors, unlike (say) VB's `on error resume next`. – Marc B Aug 23 '12 at 05:18
0

It will always echo 'connection successful' because you aren't checking the status of the connection.

if (!$myConn) {
   echo 'Sorry, there was an error '.mysql_error();
} else {
   echo 'connection successful';
}

Depending on how you executed the code, you might be running into this:

If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned. The new_link parameter modifies this behavior and makes mysql_connect() always open a new link, even if mysql_connect() was called before with the same parameters. In SQL safe mode, this parameter is ignored.

So, either do an explicit check or:

$myConn = mysql_connect($db_host, $db_username, $db_password, true) or die('Connection error: '.mysql_error());

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • I have tried that and it still prints "connection successful". From my knowledge if I either connect or die it should not execute after it has been killed. – Ryan Mouritz Aug 23 '12 at 05:19
  • This is the first connection made to this server. I think I may try to reinstall the server software. – Ryan Mouritz Aug 23 '12 at 05:28
0

try this one

$db_host = "localhost";
$db_username = "root";
$db_password = "";
$con = mysql_connect($db_host,$db_username,$db_passwd);
if (!$con)
 {
 die('Could not connect: ' . mysql_error());
 }
else
  echo 'connection successful';
  mysql_close($con);
?>
Sandy8086
  • 653
  • 1
  • 4
  • 14