PDOを使用してmysqlに接続しようとしていますが、PDOのインスタンスを作成しようとするとコードが破損します。私がしようとするとキャッチでそれを入れて試してみましたが、それはまだコードを破る/私はこのようにMySQLへの接続しようとPDOインスタンスを作成するとエラーが発生する
protected $conn;
$dsn = "mysql:host=localhost;dbname=Mydatabase";
$this->conn = new PDO($dsn, "admin", "test2016");
「localhostのページが機能していません」と言ってページを取得する:
$conn = mysqli_connect("localhost", "admin", "test2016");
正常に動作します。
新しいPDOがそのエラーを引き起こす理由はわかりません。あなたの編集に基づいて
class Database {
protected $conn;
function __construct() {
$this->init();
}
function init(){
try{
$this->conn= $this->connectDB("localhost","Mydatabase","admin","test2016"); // the here
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}catch(PDOException $e){
echo "Connection failed: " . $e->getMessage();
}
}
function connectDB($servername,$database,$username,$password){
$dsn = "mysql:host=localhost;dbname=Mydatabase";
return new PDO($dsn, $username, $password);// i am not sure why new pdo is the cause of the problem
// $conn = mysqli_connect($servername, $username, $password);
//if (!$conn) {
// die("Connection failed: " . mysqli_connect_error());
//}
//echo "Connected successfully";
}
ここにmysqli_とPDOがあるのはなぜですか?あなたがそれらを混ぜているなら、あなたはできません。 –
セミコロンをここで 'protected $ conn'と使用したいと思っています – devpro
私はそれらを同時に使用していません。私はPDOであるそれらの1つだけを使用しようとしています。 mysqlがうまくいくかどうかテストしています。 @ Fred-ii- – user629283