<?php
class DB {
private static function connect() {
$pdo = new PDO('mysqli:host=localhost;dbname=SocialNetwork;charset=utf8', 'root', '');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $pdo;
}
public static function query($query, $params = array()) {
$statement = self::connect()->prepare($query);
$statement->execute($params);
if (explode(' ', $query)[0] == 'SELECT') {
$data = $statement->fetchAll();
return $data;
}
}
}
当我尝试在数据库中插入数据时,会出现此错误:
致命错误:未捕获PDOException:在C:\ xampp \ htdocs \ social-media \ classes \ DB.php中找不到驱动程序:5堆栈跟踪:#0 C:\ xampp \ htdocs \ social-media \ classes \ DB.php(5):PDO-&gt; __ construct(&#39; mysqli:host = loc ...&#39;,&#39; root&#39;,&#39;&#39;)#1 C:\ xampp \ htdocs \ social-media \ classes \ DB.php(11):DB :: connect()#2 C:\ xampp \ htdocs \ social-media \ create-account.php(10):DB: :查询(&#39; SELECT用户名...&#39;,数组)#3 {main}在第5行的C:\ xampp \ htdocs \ social-media \ classes \ DB.php中抛出
答案 0 :(得分:0)
您需要在php.ini中激活pdo_mysql
使用extension=php_pdo_mysql.dll
查找CTRL+F
行并重新启动您的网络环境。
答案 1 :(得分:0)
驱动程序名称不正确。使用“mysql:...”
$pdo = new PDO('mysql:host=localhost;dbname=SocialNetwork;charset=utf8', 'root', '');