0

this class work with mysql perfect but i wanted to connect now with mssql server so when i try to change mysql to mssql i get error "Fatal error: Call to a member function prepare() on a non-object"

<?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();
}
}

any idea thank You

Moode Osman
  • 1,715
  • 18
  • 17

2 Answers2

0

It looks like you need to use the MSSQL specific prepare function. Just using the example from the page but you can substitute your code.

<?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));
}
TTeeple
  • 2,913
  • 1
  • 13
  • 22
0

i found the solution

here is the constractor using dblib

public function __construct() {
    $hostname = "";
    $port = ;
    $dbname = "";
    $username = "";
    $pw = "";

    // Set DSN
    $dsn = 'sqlsrv:Server=' . $this->host . ';Database=' . $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 ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw");
    }       // Catch any errors
    catch ( PDOException $e ) {
        $this->error = $e->getMessage();
    }
}
Moode Osman
  • 1,715
  • 18
  • 17