【发布时间】:2014-02-04 12:31:21
【问题描述】:
我有一个 db.php 文件 & 和 init.php 可以在我的 register.php 文件中使用。
我在 MS SQL Server 中有一个数据库,并尝试使用 PDO 连接到它。
这个错误正在显示,我不明白为什么。我使用相同的代码连接到 MySQL 数据库,这插入了记录,但写入 MSSQLServer 时没有
致命错误:未捕获的异常 'PDOException' 带有消息 'could not 在 C:\ooplr\classes\DB.php:14 中找到驱动程序' 堆栈跟踪:#0 C:\ooplr\classes\DB.php(14): PDO->__construct('mssql:host=myserver;...', 'username', 'pwd') #1 C:\ooplr\classes\DB.php(23): DB->__construct() #2 C:\ooplr\classes\Validate.php(8): DB::getInstance() #3 C:\ooplr\register.php(9): Validate->__construct() #4 {main} 抛出 C:\ooplr\classes\DB.php 第 14 行
db.php 页面
<?php
class DB {
public static $instance = null;
private $_pdo = null,
$_query = null,
$_error = false,
$_results = null,
$_count = 0;
private function __construct() {
try {
$this->_pdo = new PDO('mssql:host=' . Config::get('mssql/server') . ';dbname=' . Config::get('mssql/db'), Config::get('mssql/username'), Config::get('mssql/password'));
} catch(PDOExeption $e) {
die($e->getMessage());
}
}
public static function getInstance() {
if(!isset(self::$instance)) {
self::$instance = new DB();
}
return self::$instance;
}
public function query($sql, $params = array()) {
$this->_error = false;
if($this->_query = $this->_pdo->prepare($sql)) {
$x = 1;
if(count($params)) {
foreach($params as $param) {
$this->_query->bindValue($x, $param);
$x++;
}
}
if($this->_query->execute()) {
$this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
$this->_count = $this->_query->rowCount();
} else {
$this->_error = true;
}
}
return $this;
}
public function get($table, $where) {
return $this->action('SELECT *', $table, $where);
}
public function delete($table, $where) {
return $this->action('DELETE', $table, $where);
}
public function action($action, $table, $where = array()) {
if(count($where) === 3) {
$operators = array('=', '>', '<', '>=', '<=');
$field = $where[0];
$operator = $where[1];
$value = $where[2];
if(in_array($operator, $operators)) {
$sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?";
if(!$this->query($sql, array($value))->error()) {
return $this;
}
}
return false;
}
}
public function insert($table, $fields = array()) {
$keys = array_keys($fields);
$values = null;
$x = 1;
foreach($fields as $value) {
$values .= "?";
if($x < count($fields)) {
$values .= ', ';
}
$x++;
}
$sql = "INSERT INTO {$table} (`" . implode('`, `', $keys) . "`) VALUES ({$values})";
if(!$this->query($sql, $fields)->error()) {
return true;
}
return false;
}
public function update($table, $id, $fields = array()) {
$set = null;
$x = 1;
foreach($fields as $name => $value) {
$set .= "{$name} = ?";
if($x < count($fields)) {
$set .= ', ';
}
$x++;
}
$sql = "UPDATE users SET {$set} WHERE id = {$id}";
if(!$this->query($sql, $fields)->error()) {
return true;
}
return false;
}
public function results() {
return $this->_results;
}
public function first() {
return $this->_results[0];
}
public function count() {
return $this->_count;
}
public function error() {
return $this->_error;
}}?>
init.php 页面
<?php
session_start();
$GLOBALS['config'] = array(
'mssql' => array(
'server' => 'MyServer',
'username' => 'MyUsr',
'password' => 'Mypwd',
'db' => 'dbname'
),
'remember' => array(
'cookie_name' => 'hash',
'cookie_expiry' => 604800
),
'session' => array(
'session_name' => 'user',
'token_name' => 'token'
)
);
function autoload($class) {
require_once 'classes/' . $class . '.php';
}
spl_autoload_register('autoload');
require_once 'functions/sanitize.php';
if(Cookie::exists(Config::get('remember/cookie_name'))) {
$hash = Cookie::get(Config::get('remember/cookie_name'));
$hashCheck = DB::getInstance()->get('users_session', array('hash', '=', $hash));
if($hashCheck->count()) {
$user = new User($hashCheck->first()->user_id);
$user->login();
}}?>
register.php 页面
<?php
require 'core/init.php';
error_reporting (E_ALL);
ini_set('display_errors', TRUE);
if(Input::exists()) {
if(Token::check(Input::get('token'))) {
$validate = new Validate();
$validation = $validate->check($_POST, array(
'username' => array(
'required' => true,
'min' => 2,
'max' => 20,
'unique' => 'users'),
'password' => array(
'required' => true,
'min' => 6),
'password_again' => array(
'required' => true,
'matches' => 'password'),
'name' => array(
'required' => false,
'min' => 2,
'max' => 50)
));
if($validation->passed()) {
$user = new User();
$salt = Hash::salt(32);
try {
$user->create(array(
'username' => Input::get('username'),
'password' => Hash::make(Input::get('password'), $salt),
'salt' => $salt,
'name' => Input::get('name'),
'joined' => getdate(),
'group' => 1
));
Session::flash('home', 'You have been registered and can now log in!');
Redirect::to('index.php');
} catch(Exception $e) {
die($e->getMessage());
}
} else {
foreach($validate->errors() as $error) {
echo $error, '<br>';
}}}}?>
<form action="" method="post">
<div class="field">
<label for="username">Choose a username</label>
<input type="text" name="username" id="username" value="<?php echo escape(Input::get('username')); ?>">
</div>
<div class="field">
<label for="password">Choose a password</label>
<input type="password" name="password" id="password">
</div>
<div class="field">
<label for="password_again">Enter your password again</label>
<input type="password" name="password_again" id="password_again">
</div>
<div class="field">
<label for="name">Your name</label>
<input type="text" name="name" id="name" value="<?php echo escape(Input::get('name')); ?>">
</div>
<input type="submit" value="Register">
<input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
</form>
我怎样才能让它连接和发送数据?
【问题讨论】:
-
PDO没有mssql的驱动程序。 please see the manual on how to install it. -
谢谢,我已经成功连接了 :-) 我正在查看我现在遇到的哈希问题!
标签: php mysql sql sql-server pdo