简体   繁体   中英

PDO MySQL connection troubleshooting

I'm running the following very basic PDO:MySql connection:

<?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";
  ?>

I've modified php.ini to allow for extension=php_pdo_mysql.dll with no effect. Is there something else that I'm missing. I am using PHP7.0.3. The error message output is Error!: could not find driver Kudos!

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

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

问题是使用msqli而不是mysql

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