2012-06-14 27 views
0

我使用PHP創建了與mySQL數據庫的數據庫連接。它工作正常。 現在,我想改變連接到MS SQL Server數據庫與以前的MySQL數據庫相同的表。如何將PHP數據庫連接從MySQL更改爲MS SQL Server?

下面是代碼:
的config.php

<?php 
/** 
* Database config variables 
*/ 
define("DB_HOST", "localhost"); 
define("DB_USER", "root"); 
define("DB_PASSWORD", ""); 
define("DB_DATABASE", "android_api"); 
?> 


DB_Connect.php

<?php 
class DB_Connect { 

    // constructor 
    function __construct() { 

    } 

    // destructor 
    function __destruct() { 
     // $this->close(); 
    } 

    // Connecting to database 
    public function connect() { 
     require_once 'include/config.php'; 
     // connecting to mysql 
     $con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); 
     // selecting database 
     mysql_select_db(DB_DATABASE); 

     // return database handler 
     return $con; 
    } 

    // Closing database connection 
    public function close() { 
     mysql_close(); 
    } 

} 

?> 


DB_Functions.php

<?php 

class DB_Functions { 

    private $db; 

    //put your code here 
    // constructor 
    function __construct() { 
     require_once 'DB_Connect.php'; 
     // connecting to database 
     $this->db = new DB_Connect(); 
     $this->db->connect(); 
    } 

    // destructor 
    function __destruct() { 

    } 

    /** 
    * Storing new user 
    * returns user details 
    */ 
    public function storeUser($name, $email, $password) { 
     $uuid = uniqid('', true); 
     $hash = $this->hashSSHA($password); 
     $encrypted_password = $hash["encrypted"]; // encrypted password 
     $salt = $hash["salt"]; // salt 
     $result = mysql_query("INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES('$uuid', '$name', '$email', '$encrypted_password', '$salt', NOW())"); 
     // check for successful store 
     if ($result) { 
      // get user details 
      $uid = mysql_insert_id(); // last inserted id 
      $result = mysql_query("SELECT * FROM users WHERE uid = $uid"); 
      // return user details 
      return mysql_fetch_array($result); 
     } else { 
      return false; 
     } 
    } 

    /** 
    * Get user by email and password 
    */ 
    public function getUserByEmailAndPassword($email, $password) { 
     $result = mysql_query("SELECT * FROM users WHERE email = '$email'") or die(mysql_error()); 
     // check for result 
     $no_of_rows = mysql_num_rows($result); 
     if ($no_of_rows > 0) { 
      $result = mysql_fetch_array($result); 
      $salt = $result['salt']; 
      $encrypted_password = $result['encrypted_password']; 
      $hash = $this->checkhashSSHA($salt, $password); 
      // check for password equality 
      if ($encrypted_password == $hash) { 
       // user authentication details are correct 
       return $result; 
      } 
     } else { 
      // user not found 
      return false; 
     } 
    } 

    /** 
    * Check user is existed or not 
    */ 
    public function isUserExisted($email) { 
     $result = mysql_query("SELECT email from users WHERE email = '$email'"); 
     $no_of_rows = mysql_num_rows($result); 
     if ($no_of_rows > 0) { 
      // user existed 
      return true; 
     } else { 
      // user not existed 
      return false; 
     } 
    } 

    /** 
    * Encrypting password 
    * @param password 
    * returns salt and encrypted password 
    */ 
    public function hashSSHA($password) { 

     $salt = sha1(rand()); 
     $salt = substr($salt, 0, 10); 
     $encrypted = base64_encode(sha1($password . $salt, true) . $salt); 
     $hash = array("salt" => $salt, "encrypted" => $encrypted); 
     return $hash; 
    } 

    /** 
    * Decrypting password 
    * @param salt, password 
    * returns hash string 
    */ 
    public function checkhashSSHA($salt, $password) { 

     $hash = base64_encode(sha1($password . $salt, true) . $salt); 

     return $hash; 
    } 

} 

?> 


的index.php

<?php 

/** 
* File to handle all API requests 
* Accepts GET and POST 
* 
* Each request will be identified by TAG 
* Response will be JSON data 

    /** 
* check for POST request 
*/ 
if (isset($_POST['tag']) && $_POST['tag'] != '') { 
    // get tag 
    $tag = $_POST['tag']; 

    // include db handler 
    require_once 'include/DB_Functions.php'; 
    $db = new DB_Functions(); 

    // response Array 
    $response = array("tag" => $tag, "success" => 0, "error" => 0); 

    // check for tag type 
    if ($tag == 'login') { 
     // Request type is check Login 
     $email = $_POST['email']; 
     $password = $_POST['password']; 

     // check for user 
     $user = $db->getUserByEmailAndPassword($email, $password); 
     if ($user != false) { 
      // user found 
      // echo json with success = 1 
      $response["success"] = 1; 
      $response["uid"] = $user["unique_id"]; 
      $response["user"]["name"] = $user["name"]; 
      $response["user"]["email"] = $user["email"]; 
      $response["user"]["created_at"] = $user["created_at"]; 
      $response["user"]["updated_at"] = $user["updated_at"]; 
      echo json_encode($response); 
     } else { 
      // user not found 
      // echo json with error = 1 
      $response["error"] = 1; 
      $response["error_msg"] = "Incorrect email or password!"; 
      echo json_encode($response); 
     } 
    } else if ($tag == 'register') { 
     // Request type is Register new user 
     $name = $_POST['name']; 
     $email = $_POST['email']; 
     $password = $_POST['password']; 

     // check if user is already existed 
     if ($db->isUserExisted($email)) { 
      // user is already existed - error response 
      $response["error"] = 2; 
      $response["error_msg"] = "User already existed"; 
      echo json_encode($response); 
     } else { 
      // store user 
      $user = $db->storeUser($name, $email, $password); 
      if ($user) { 
       // user stored successfully 
       $response["success"] = 1; 
       $response["uid"] = $user["unique_id"]; 
       $response["user"]["name"] = $user["name"]; 
       $response["user"]["email"] = $user["email"]; 
       $response["user"]["created_at"] = $user["created_at"]; 
       $response["user"]["updated_at"] = $user["updated_at"]; 
       echo json_encode($response); 
      } else { 
       // user failed to store 
       $response["error"] = 1; 
       $response["error_msg"] = "Error occured in Registartion"; 
       echo json_encode($response); 
      } 
     } 
    } else { 
     echo "Invalid Request"; 
    } 
} else { 
    echo "Access Denied"; 
} 
?> 
+5

MySQL庫僅適用於** MySQL **(因此名稱)。您需要使用PDO或類似的連接到不同的供應商。檢查出http://www.php.net/manual/en/refs.database.php –

+0

+1爲PDO,definitly正確的選項 – PEM

+0

看起來像'gimme代碼'的問題... – Repox

回答

5

如果我是你,我會寫兩個不同的數據庫類,有些看起來像這樣:

interface DBConnection { 
    public function connect(); 
    public function query($sql); 
    public function fetch($query); 
    // etc whatever else you need 
} 

class MySQLConnection implements DBConnection { 
    // implement these using all the MySQL funcs 
} 

class MSSQLConnection implements DBConnection { 
    // implement these using all the MSSQL funcs 
}  

然後,不使用的mysql_query(),mysql_fetch_array( )和DB_Functions中的類似,使用$ db-> query()和$ db-> fetch(),而$ db包含MySQLConnection或MSSQLConnection的實例,具體取決於要使用哪個實例。該接口將確保您在兩個類中都實現了所有相同的功能。

/編輯:當然,我很愚蠢:Tieso T是對的,應該使用PDO。這可能會更加有效......

+0

謝謝你的回答,我想我會先使用PDO :) – blankon91

相關問題