PDO MySQL连接故障排除

时间:2016-03-05 19:36:36

标签: php mysql pdo

我正在运行以下非常基本的PDO:MySql连接:

<?php

  $host = '127.0.0.1:8888';
  $db = 'communities';
  $user = 'root';
  $pass = 'yY5MF)q/DCzc';
  // Create connection
  try {
  $conn = new PDO('mysqli:host=$host;dbname=$db', $user, $pass);
  foreach($conn->query('SELECT * from city_detail') as $row) {
    print_r($row);
  }
  $conn = null;
  // Check connection
  } catch (PDOException $e) {
  print "Error!: " . $e->getMessage() . "<br/>";
  die();
}
  echo "Connected successfully";
  ?>

我已修改php.ini以允许extension=php_pdo_mysql.dll无效。还有其他我想念的东西。我使用的是PHP7.0.3。错误消息输出为Error!: could not find driver Kudos!

2 个答案:

答案 0 :(得分:2)

您需要使用双引号来获取字符串中的可变数据:

$conn = new PDO("mysqli:host=$host;dbname=$db", $user, $pass);

答案 1 :(得分:0)

问题在于使用msqli而不是mysql