[英]PHP PDO connection with MS SQL Server using ODBC
I have a db.php file & and init.php to use within my register.php file. 我的register.php文件中有一个db.php文件和init.php。
I have a database within MS SQL Server and am trying to connect to it using PDO. 我在MS SQL Server中有一个数据库,正在尝试使用PDO连接到它。
This error is showing and I can't see why. 此错误正在显示,我看不到为什么。 I used the same code to connect to a MySQL db and this inserted the record as should, but doesn't when writing to a MSSQLServer
我使用相同的代码连接到MySQL数据库,并按需插入了记录,但写入MSSQLServer时未插入
Fatal error: Uncaught exception 'PDOException' with message 'could not find driver' in C:\\ooplr\\classes\\DB.php:14 Stack trace: #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} thrown in C:\\ooplr\\classes\\DB.php on line 14
致命错误:C:\\ ooplr \\ classes \\ DB.php:14中消息未找到驱动程序的未捕获异常'PDOException':堆栈跟踪:#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行
The db.php page 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;
}}?>
The init.php page 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();
}}?>
The register.php page 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>
How can I get this to connect and send the data? 如何获得此连接并发送数据?
You error message says that PHP/PDO could not find a driver for SQL Server. 您的错误消息说PHP / PDO找不到SQL Server驱动程序。 This happens because PDO does not support SQL Server drivers out of the box.
发生这种情况是因为PDO不支持现成的SQL Server驱动程序 。
To get SQL Server support you will need to add Microsoft Drivers for PHP for SQL Server to your PHP installation. 要获得SQL Server支持,您需要在PHP安装中添加SQL Server的Microsoft驱动程序 。 This will only be possible if you have control over the PHP installation on the server (this is possible on a local server but not possible in a shared hosting environment).
仅当您可以控制服务器上的PHP安装时,这才可能(在本地服务器上可以,但在共享主机环境中则不能)。
Alternatively, you may consider using PHP's MSSQL Database Extension that does support Microsoft SQL Server. 或者,您可以考虑使用确实支持Microsoft SQL Server的PHP的MSSQL数据库扩展 。 CodeIgniter uses the MSSQL database extension in their MSSQL database driver .
CodeIgniter在其MSSQL数据库驱动程序中使用MSSQL数据库扩展。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.