简体   繁体   中英

Unable To Connect Database MySql

I'm New To PHP PDO and I have written following Code on Config.php

class DBConnect{
    /* defining constants for database connectivity  */
    private $host="127.0.0.1";
    private $user="root";
    private $pass="";
    private $dbname="sms-portal";

    private $dbh;
    private $error;

    public function __construct(){
        // set DSN
        $dsn='mysql:host='.$this->host.'dbname='.$this->dbname;
        //set Options

        $options= array(
            PDO::ATTR_PERSISTENT => true,
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
        );

        // Create New PDO instance
        try{
            $this->dbh=new PDO($dsn, $this->user, $this->pass, $options);
            //die("here");
        }
        // Catch any errors here
        catch(PDOException $e){
            $this->error=$e->getMessage();
        }   
    }
}

Whilst working on localhost and Submitting NewUserRegistration form Its Showing Me following Warning Message.

Warning: PDO::__construct() [pdo.--construct]: 
php_network_getaddresses: getaddrinfo failed: 
No such host is known. in config.php on line 25

Warning: PDO::__construct() [pdo.--construct]: 
[2002] php_network_getaddresses: getaddrinfo failed: 
No such host is kn (trying to connect via tcp://127.0.0.1dbname=sms-portal:3306) 
    in config.php on line 25

$dsn='mysql:host='.$this->host.'dbname='.$this->dbname;

看起来您在dbname =之前缺少分号-尝试以下操作:

$dsn='mysql:host='.$this->host.';dbname='.$this->dbname;

  $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);

Note : You just missed ";" between host and dbname.

Add the ; semicolon before the dbname :

$dsn='mysql:host='.$this->host.';dbname='.$this->dbname;
                            //  ^ this one

If you look at the error, the host isn't terminated well:

tcp://127.0.0.1dbname=sms-portal:3306

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM