1

I've got my own DB.php and it was working before I changed the MySQL Port to something else other than 3306 which is default. It fails to connect now; I've configured the DB.php after port change like below :

class DB {
//    server    
    private $host = "192.168.1.15";
    private $uname = "user";
    private $pw = "iamuser";
    private $dbname = "matlab";
    private $con = null;
    private $port = "669";

public function __construct() {
        $this->con = mysql_connect($this->host,$this->port, $this->uname, $this->pw);
        if (!$this->con) {
            $this->db_error("Cannot connect to DBMS");
        }
        mysql_query("SET NAMES 'latin5'", $this->con);
        mysql_query("SET CHARACTER SET 'latin5'", $this->con);
        mysql_query("SET COLLATION_CONNECTION = 'latin5_turkish_ci'", $this->con);
        mysql_select_db($this->dbname, $this->con) or db_error("Can not connect to Database");
    }

Can anyone tell me how I can make it work with the port number 669 again? Thanks.

  • Did you change your mysql port to which? If you want to know what is the MySQL port, check [this](http://stackoverflow.com/a/18353323/1992780). – Davide Pastore Nov 20 '14 at 09:14

2 Answers2

3

The Fix:

You are passing wrong parameter to mysql_connect function, port is not a separate parameter. First parameter accept values as [hostname:port]. So it should be:

mysql_connect($this->host.':'.$this->port, $this->uname, $this->pw);

Reference:

http://php.net/manual/en/function.mysql-connect.php

P.S.

Note: MySQL library is deprecated please use MySQLi or PDO instead

Qarib Haider
  • 4,796
  • 5
  • 27
  • 38
  • Solved it. I'll also consider your suggestion of MySQLi and PDO. Thank you. –  Nov 20 '14 at 11:26
1

Here is the PDO way.

class Db extends \PDO {
    public function __construct() {
        $aDriverOptions[\PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES UTF8';
        parent::__construct('mysql:host=' . Config::DB_HOST . ':80;port=3307;dbname=' . Config::DB_NAME . ';', Config::DB_USR, Config::DB_PWD, $aDriverOptions);
        $this->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
    }
}

The most important part is the first parameter of the constructor 'mysql:host=xxx;port=xxx;dbname=xxx' where you have to specify your other-than-3306 port number. In my case, I used 3307.

Serge Kishiko
  • 466
  • 1
  • 6
  • 13