在 php 中使用 PDO 连接到数据库
Connecting to database with PDO in php
我为项目中的数据库连接编写了这个小 class:
<?php
class DatabaseUtility{
private $dsn, $username, $password, $database, $pdo;
public function __construct($host = 'localhost', $username = 'root', $password = '', $database){
$this->dsn = "mysqli:dbname=$database;host:$host";
$this->username = $username;
$this->password = $password;
$this->database = $database;
}
public function connect(){
try{
$this->pdo = new PDO($this->dsn,$this->username,$this->password,null);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
} catch(PDOException $err){
die($err->getMessage());
}
}
public function prepareStatment($query){
$this->pdo->prepare($query);
}
}
?>
这就是我的使用方式:
<?php
require 'DatabaseUtility.php';
$db = new DatabaseUtility('localhost','root','','apex');
$db->connect();
$statment = $db->prepareStatment("Select offer_id from offer_images where img_id = :img_id");
?>
但我收到以下错误:
Could not find driver
我是 PDO 的新手所以请指导我我做错了什么?这种方法对于安全快速的数据库是否可行 activity?
更新:
我现在使用这些代码行来使用我的 DatabaseUtility class 但出现错误:
<?php
require 'DatabaseUtility.php';
$id= 25;
$db = new DatabaseUtility('localhost','root','','apex');
$db->connect();
$statment = $db->prepareStatment("Select offer_id from offer_images where img_id = :img_id");
$statment->bindParam("img_id", $id ,PDO::PARAM_INT);
$statment->execute();
print_r($statment);
?>
错误是:
call to a member function bindParam() on a non-object in this line:
$statment->bindParam("img_id", $id ,PDO::PARAM_INT);
所以你似乎在混合 MYSQL
as database with the MYSQL API's in PHP e.g. PDO
, mysqli_*
或 mysql_*
。
您使用 PDO
作为 API 连接到您的 MYSQL 数据库。但是你的 connection 字符串中有一些小错误:
//vvvvv < -- > vvvvvvvvv
$this->dsn = "mysqli:host=$host;dbname:$database";
//^ ^^^^ < - > ^^^^^^^ must be a equal sign
//| Your database is MYSQL so remove the i
您的 prepareStatment()
方法似乎没有返回任何内容。
public function prepareStatment($query){
return $this->pdo->prepare($query);
}
这就是 $statment = $db->prepareStatment("Select offer_id from offer_images where img_id = :img_id");
返回 false 的原因。
我为项目中的数据库连接编写了这个小 class:
<?php
class DatabaseUtility{
private $dsn, $username, $password, $database, $pdo;
public function __construct($host = 'localhost', $username = 'root', $password = '', $database){
$this->dsn = "mysqli:dbname=$database;host:$host";
$this->username = $username;
$this->password = $password;
$this->database = $database;
}
public function connect(){
try{
$this->pdo = new PDO($this->dsn,$this->username,$this->password,null);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
} catch(PDOException $err){
die($err->getMessage());
}
}
public function prepareStatment($query){
$this->pdo->prepare($query);
}
}
?>
这就是我的使用方式:
<?php
require 'DatabaseUtility.php';
$db = new DatabaseUtility('localhost','root','','apex');
$db->connect();
$statment = $db->prepareStatment("Select offer_id from offer_images where img_id = :img_id");
?>
但我收到以下错误:
Could not find driver
我是 PDO 的新手所以请指导我我做错了什么?这种方法对于安全快速的数据库是否可行 activity?
更新: 我现在使用这些代码行来使用我的 DatabaseUtility class 但出现错误:
<?php
require 'DatabaseUtility.php';
$id= 25;
$db = new DatabaseUtility('localhost','root','','apex');
$db->connect();
$statment = $db->prepareStatment("Select offer_id from offer_images where img_id = :img_id");
$statment->bindParam("img_id", $id ,PDO::PARAM_INT);
$statment->execute();
print_r($statment);
?>
错误是:
call to a member function bindParam() on a non-object in this line:
$statment->bindParam("img_id", $id ,PDO::PARAM_INT);
所以你似乎在混合 MYSQL
as database with the MYSQL API's in PHP e.g. PDO
, mysqli_*
或 mysql_*
。
您使用 PDO
作为 API 连接到您的 MYSQL 数据库。但是你的 connection 字符串中有一些小错误:
//vvvvv < -- > vvvvvvvvv
$this->dsn = "mysqli:host=$host;dbname:$database";
//^ ^^^^ < - > ^^^^^^^ must be a equal sign
//| Your database is MYSQL so remove the i
您的 prepareStatment()
方法似乎没有返回任何内容。
public function prepareStatment($query){
return $this->pdo->prepare($query);
}
这就是 $statment = $db->prepareStatment("Select offer_id from offer_images where img_id = :img_id");
返回 false 的原因。