0

I am getting problem in connect to mysql server. I installed PHP fast cgi with iis 7 via Web installer platform of windows. Everything is ok until i'm trying to connect mysql server, it's alway fail. Here is my code :

class MySqlDatabase{
    private $mysql;
    private $host;
    private $username;
    private $password;
    private $db_name;

    function __construct($host, $username, $password, $db_name){
        $this->host = $host;
        $this->username = $username;
        $this->password = $password;
        $this->db_name = $db_name;
    }
    public function connect(){
        $this->mysql = mysql_connect($this->host, $this->username, 
                       $this->password) or 
                       die("Could not connect to database. ". $this->username . 
                       "@" . $this->host . " passwrod: YES!");
        if (!mysql_select_db($this->db_name))   die("Cannot select database!");
    }
}
$database = new MySqlDataBase($host,$username,$password,$db_name);
$database->connect();
Yogesh Pingle
  • 3,545
  • 3
  • 16
  • 16
user1982667
  • 1
  • 1
  • 1
  • check your mysql username and password is correct – Anooj P Jan 16 '13 at 07:09
  • i checked username and password by connect to mysql server via SQLyog software, everything is ok, i connected. I think i got problem about config iis maybe – user1982667 Jan 16 '13 at 07:13
  • If you don't see any errors or warnings, enable them temporarily in your `php.ini` file using the `DisplayErrors` directive. – Luka Jan 16 '13 at 07:19
  • 1
    fwiw, the `mysql_*` functions are deprecated. if you want an OO interface to your database, try out [PDO](http://php.net/manual/en/book.pdo.php) – Eevee Jan 16 '13 at 07:28
  • _this is what you will get... this is what you will get... when you use `mysql_*`_ (Radiohead). `mysql_*` extension is being deprecated and shouldn't be used to write new code anymore. Look into `PDO` for a safer, better OO DB extension, or (if needs must) `mysqli_*`. Note the `i`, it stands for _improved_ – Elias Van Ootegem Jan 16 '13 at 07:33

3 Answers3

1
  1. Make sure your MySql service is running.
  2. Make sure your credentials are correct, try with MySql Workbench to see if you have a connection.
  3. Make sure your script has the correct credentials as well.
  4. Make sure your script targets the machine where the service is running, e.g. localhost port 3306, just like MySql Workbench.
Mike de Klerk
  • 11,906
  • 8
  • 54
  • 76
0

You obviously wrong credentials to connect to the database. That's why you get the error " Could not connect to MySQL". Check your variables and your username and password you send for the connection to be achieved.

user1982640
  • 11
  • 1
  • 3
0

Try to do this, without class declaration:

// we connect to localhost at port 3307
$link = mysql_connect('127.0.0.1:3307', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
RDK
  • 4,540
  • 2
  • 20
  • 29
  • i've got message error: Could not connect: mysqlnd cannot connect to MySQL 4.1+ using the old insecure authentication. Please use an administration tool to reset your password with the command SET PASSWORD = PASSWORD('your_existing_password'). This will store a new, and more secure, hash value in mysql.user. If this user is used in other scripts executed by PHP 5.2 or earlier you might need to remove the old-passwords flag from your my.cnf file – user1982667 Jan 16 '13 at 07:25
  • Try to reset mysql user password or restart mysql server on CentOS: /etc/init.d/mysqld restart on Debian: /etc/init.d/mysql restart – RDK Jan 16 '13 at 07:27
  • thanks all, i've solved my problem at http://stackoverflow.com/questions/8831183/mysqlnd-cannot-connect-to-mysql-4-1-using-the-old-insecure-authentication?answertab=votes#tab-top – user1982667 Jan 16 '13 at 07:49