簡體   English   中英

使MySQL的PHP​​代碼與MsSQL一起使用

[英]Make PHP code with MySQL work with MsSQL

我們有一組學生開發的項目,作為他們的碩士學位最終項目的一部分。 整個解決方案效果很好,很遺憾,它是針對MySQL數據庫開發的,而我們使用的是MsSQL Server數據庫。

我一直在努力等待使它正常工作,但我並沒有真正走到哪里。 在本地計算機上,我正在運行具有PHP 5.3.29和Sql Server 2012的Apache服務器。

來自phpinfo()的信息; Apache版本Apache / 2.2.25(Win32)PHP / 5.3.29我在phpinfo()中看不到mssql,但是在PHP.ini中,我確實有以下內容:

[MSSQL]
; Allow or prevent persistent links.
mssql.allow_persistent = On

; Maximum number of persistent links.  -1 means no limit.
mssql.max_persistent = -1

; Maximum number of links (persistent+non persistent).  -1 means no limit.
mssql.max_links = -1

; Minimum error severity to display.
mssql.min_error_severity = 10

; Minimum message severity to display.
mssql.min_message_severity = 10

; Compatibility mode with old versions of PHP 3.0.
mssql.compatability_mode = Off

; Connect timeout
;mssql.connect_timeout = 5

; Query timeout
;mssql.timeout = 60

; Valid range 0 - 2147483647.  Default = 4096.
;mssql.textlimit = 4096

; Valid range 0 - 2147483647.  Default = 4096.
;mssql.textsize = 4096

; Limits the number of records in each batch.  0 = all records in one batch.
;mssql.batchsize = 0

; Specify how datetime and datetim4 columns are returned
; On => Returns data converted to SQL server settings
; Off => Returns values as YYYY-MM-DD hh:mm:ss
;mssql.datetimeconvert = On

; Use NT authentication when connecting to the server
mssql.secure_connection = On

; Specify max number of processes. -1 = library default
; msdlib defaults to 25
; FreeTDS defaults to 4096
;mssql.max_procs = -1

; Specify client character set.
; If empty or not set the client charset from freetds.conf is used
; This is only used when compiled with FreeTDS
;mssql.charset = "ISO-8859-1"

我嘗試了以下方法:dbconnect.php

$myServer = "localhost";
$myUser = "sa";
$myPass = "sa123";
$myDB = "st"; 

//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
  or die("Couldn't connect to SQL Server on $myServer"); 

die();  
  $selected = mssql_select_db($myDB, $dbhandle)
  or die("Couldn't open database $myDB"); 

我首先試圖讓系統甚至允許我登錄,所以login.php代碼如下。 我已將查詢更改為mssql:

    <?php
error_reporting(E_ALL);
session_start(); // Starting Session
require("includes/db_connect.php");
$hint = "";
$username=$_POST["username"];
$password=$_POST["password"];
/*=============================================================
                    SQL INJECTION PREVENTION
===============================================================*/
$PRElist = array();
$PREsql = "SELECT Username, Password FROM tblUsers ;";
$PREresult = mssql_query($PREsql);
//if (mysqli_num_rows($PREresult)>0) 
if (1 == 1){
    // output data of each row
    while($row = mssql_fetch_assoc($PREresult)) {
        $PRElist[]= strtolower($row['Username']);
        $PRElist[strtolower($row['Username'])]=$row['Password'];
    }
}//to prevent sql injection
//=======================START LOOKING UP THE USER==================
if ((in_array(strtolower($username), $PRElist))&&($PRElist[strtolower($username)]==$password)) 
    {
        $sql = "SELECT UserId, Username, Password FROM tblUsers where Username='$username' AND Password='$password'";
        $result = mssql_query($sql);
        $numRows = mssql_num_rows($result); 
        if ($numRows > 0) {
            // output data of each row
            while($row = mssql_fetch_assoc($result)) {
                $hint="";                   //initialize the hint string.. 
                if (strtolower($username)==strtolower($row["Username"])){
                    $userID= $row["UserId"];
                    $sql = "SELECT GroupId FROM tblUserGroups where UserId='$userID'";
                    $result = mssql_query($sql);
                    $numRows1 = mssql_num_rows($result); 
                    if ($numRows1 > 0) {
                        // output data of each row
                        while($row = mssql_fetch_assoc($result)) {
                            switch ($row["GroupId"]) {
                                case '1':
                                        header("location: home.php"); // Redirecting To Other Page
                                        $hint="<span style='color:green'> This username is registered </span>";
                                        $_SESSION['login_user']=$username; // Initializing Session
                                        $_SESSION['login_pass']=$password; // Initializing Session# code...
                                        $_SESSION['userID']=$userID; // Initializing Session# code...
                                    break;
                                case '2':
                                        header("location: Team_Home.php"); // Redirecting To Other Page
                                        $hint="<span style='color:green'> This username is registered </span>";
                                        $_SESSION['login_user']=$username; // Initializing Session
                                        $_SESSION['login_pass']=$password; // Initializing Session# code...
                                        $_SESSION['userID']=$userID; // Initializing Session# code...
                                    break;
                                case '3':
                                        header("location: Staff_Home.php"); // Redirecting To Other Page
                                        $hint="<span style='color:green'> This username is registered </span>";
                                        $_SESSION['login_user']=$username; // Initializing Session
                                        $_SESSION['login_pass']=$password; // Initializing Session# code...
                                        $_SESSION['userID']=$userID; // Initializing Session# code...
                                    break;
                                default:
                                        $hint="<span style='color:red'>Not registered...</span>";
                                        header("location: index.php"); // Redirecting To Other Page
                                    break;
                            }
                        }
                    }


                }
                else
                {
                    $hint="<span style='color:red'>Not registered...</span>";
                    header("location: index.php"); // Redirecting To Other Page

                }
            }
        } 
    }
    else{
        header("location: index.php"); // Redirecting To Other Page
        $hint="<span style='color:red'>Not registered...</span>";
    }
    echo $hint;
    mssql_close($conn);

我無法真正看到發生了什么,因為當我嘗試登錄時,我只得到了一個白色屏幕,控制台中沒有任何信息或錯誤。

$myServer = "localhost";
$myUser = "sa";
$myPass = "sa123";
$myDB = "st"; 

//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
  or die("Couldn't connect to SQL Server on $myServer"); 

die();   // whats this for? it can cause white screen.**



$selected = mssql_select_db($myDB, $dbhandle)
  or die("Couldn't open database $myDB");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM