我是PHP PDO的新手,我在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();
}
}
}
同时处理localhost并提交NewUserRegistration形成其显示我的警告消息。
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
答案 0 :(得分:2)
$dsn='mysql:host='.$this->host.'dbname='.$this->dbname;
在dbname =之前看起来你错过了一个分号 - 试试这个:
$dsn='mysql:host='.$this->host.';dbname='.$this->dbname;
答案 1 :(得分:2)
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
注意:你错过了#34 ;;"在host和dbname之间。
答案 2 :(得分:1)
在;
:
dbname
分号
$dsn='mysql:host='.$this->host.';dbname='.$this->dbname;
// ^ this one
如果您查看错误,主机不能很好地终止:
tcp://127.0.0.1dbname=sms-portal:3306