0

I'm very new to OOP and am trying to learn it. So please excuse my noobness. I'm trying to connect to mysql and to test whether the connection is successful or not, I'm using if-else conditions.

Surprisingly, the mysql_connect is always returning true even on passing wrong login credentials. Now I'm trying to figure out why it does and after spending about 20 minutes, I gave up. Hence, I came here to seek the help of the community. Here is my code:

class test
{
    private $host = 'localhost';
    private $username = 'root2'; // using wrong username on purpose
    private $password = '';
    private $db = 'dummy';
    private $myConn;    

    public function __construct()
    {
       $conn = mysql_connect($this->host, $this->username, $this->password);
       if(!$conn)
       {
        die('Connection failed'); // this doesn't execute
       }
       else
       {
        $this->myConn = $conn;
        $dbhandle = mysql_select_db($this->db, $this->myConn);
        if(! $dbhandle)
        {
            die('Connection successful, but database not found'); // but this gets printed instead
        }
       }        
    }
}

$test = new test();
Florent
  • 12,310
  • 10
  • 49
  • 58
403 Forbidden
  • 91
  • 1
  • 4
  • your problem has absolutely nothing to do with OOP – Carlos Campderrós Dec 11 '13 at 13:42
  • try this instead `$conn = mysql_connect($this->host, $this->username, $this->password) or die(mysql_error());` sorry my php is a bit rusty – Armand Dec 11 '13 at 13:43
  • 4
    Especially as you are learning [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](http://j.mp/XqV7Lp). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). If you follow it you should have a successful connection to your DB. – vascowhite Dec 11 '13 at 13:44
  • returns true? Not on my installations it doesn't. Since I can't replicate the problem, and what you describe is what authentication is explicitly intended to avoid, I can't tell you why your isn't working. – symcbean Dec 11 '13 at 13:45
  • @Armand Doesn't work either. Still goes to second `die` (mysql_select_db) – 403 Forbidden Dec 11 '13 at 13:53
  • @symcbean If I echo `$conn`, I'm getting `Resource id #3` even with wrong login information. That shouldn't be the case right? – 403 Forbidden Dec 11 '13 at 13:55
  • @403 Forbidden: That's what I said: either your analysis is wrong or there is something very wrong with *your* installation. – symcbean Dec 11 '13 at 14:05

2 Answers2

0

Please don't use the mysql_* functions, there are many, many reasons why - which are well documented online. They are also deprecated and due to be removed.

You'd be much better off using PDO!

Also I'd strongly advise abstracting this database code into a dedicated database class, which can be injected where necessary.

On-topic:

That code snippet seems to work for me, have you tried var_dumping $conn? Does that user have correct rights?

I also hope that you don't have a production server which allows root login without a password!

Tom Green
  • 373
  • 4
  • 18
0

Ignoring the fact that you're using mysql_* functions rather than mysqli or pdo functions, you should utilise exceptions in OOP code rather than die(). Other than that, I can't replicate your problem - it may be that your mysql server is set up to accept passwordless logins.

class test
{
    private $host = 'localhost';
    private $username = 'root2'; // using wrong username on purpose
    private $password = '';
    private $db = 'dummy';
    private $myConn;    

    public function __construct()
    {
       // returns false on failure
       $conn = mysql_connect($this->host, $this->username, $this->password);
       if(!$conn)
       {
        throw new RuntimeException('Connection failed'); // this doesn't execute
       }
       else
       {
        $this->myConn = $conn;
        $dbhandle = mysql_select_db($this->db, $this->myConn);
        if (!$dbhandle)
        {
            throw new RuntimeException('Connection successful, but database not found'); // but this gets printed instead
        }
       }        
    }
}

try {
$test = new test();
} catch (RuntimeException $ex) {
    die($ex->getMessage());
}
kguest
  • 3,804
  • 3
  • 29
  • 31