php - 我如何将此类转换为与 mssql 一起使用

标签 php mysql sql-server

这个类与mysql完美配合 但我现在想连接 mssql 服务器 所以当我尝试将 mysql 更改为 mssql 时出现错误 “ fatal error :在非对象上调用成员函数prepare()”

<?php
class Database {
private $host = DB_HOST;
private $user = DB_USER;
private $pass = DB_PASS;
private $dbname = DB_NAME;

private $dbh;
private $error;
private $stmt;

public function __construct() {
    // Set DSN
    $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
    // Set options
    $options = array (
            PDO::ATTR_PERSISTENT => true,
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION 
    );
    // Create a new PDO instanace
    try {
        $this->dbh = new PDO ($dsn, $this->user, $this->pass, $options);
    }       // Catch any errors
    catch ( PDOException $e ) {
        $this->error = $e->getMessage();
    }
}


public function query($query) {
    $this->stmt = $this->dbh->prepare($query);
}


public function bind($param, $value, $type = null) {
    if (is_null ( $type )) {
        switch (true) {
            case is_int ( $value ) :
                $type = PDO::PARAM_INT;
                break;
            case is_bool ( $value ) :
                $type = PDO::PARAM_BOOL;
                break;
            case is_null ( $value ) :
                $type = PDO::PARAM_NULL;
                break;
            default :
                $type = PDO::PARAM_STR;
        }
    }
    $this->stmt->bindValue ( $param, $value, $type );
}


public function execute(){
    return $this->stmt->execute();
}


public function resultset(){
    $this->execute();
    return $this->stmt->fetchAll(PDO::FETCH_OBJ);
}


public function single(){
    $this->execute();
    return $this->stmt->fetch(PDO::FETCH_OBJ);
}


public function rowCount(){
    return $this->stmt->rowCount();
}


public function lastInsertId(){
    return $this->dbh->lastInsertId();
}


public function beginTransaction(){
    return $this->dbh->beginTransaction();
}


public function endTransaction(){
    return $this->dbh->commit();
}


public function cancelTransaction(){
    return $this->dbh->rollBack();
}
}

任何想法谢谢

最佳答案

看来您需要使用 MSSQL specific prepare功能。仅使用页面中的示例,但您可以替换您的代码。

<?php
$serverName = "serverName\sqlexpress";
$connectionInfo = array( "Database"=>"dbName", "UID"=>"username", "PWD"=>"password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false) {
    die( print_r( sqlsrv_errors(), true));
}

$sql = "UPDATE Table_1
        SET OrderQty = ?
        WHERE SalesOrderID = ?";

// Initialize parameters and prepare the statement. 
// Variables $qty and $id are bound to the statement, $stmt.
$qty = 0; $id = 0;
$stmt = sqlsrv_prepare( $conn, $sql, array( &$qty, &$id));
if( !$stmt ) {
    die( print_r( sqlsrv_errors(), true));
}

关于php - 我如何将此类转换为与 mssql 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32233498/

相关文章:

php - MYSQL 选择 'random' 行,但如果再次调用,总是给出相同的随机行

php - 在php中匹配并访问带有数据库列的字符串

php - 在保留键的同时使用排序

sql - 获取 3 天前的日期

.net - 将表行从一个表移动到另一个表的最有效方法

php - htaccess 重写 url,如 Stack Overflow

mysql - 服务器是否为本地主机 :3306 an issue? phpMyAdmin

PHP-UPLOAD_ERR_NO_TMP_DIR

php - 使用 PHP 从 SQL 数据库中获取信息

c# - 批量插入记录到 SQL Server 数据库