我們已經有一個由一羣學生開發的項目,作爲他們碩士學位最終項目的一部分。整體解決方案效果很好,不幸的是它是爲MySQL數據庫開發的,我們正在使用MsSQL服務器數據庫。讓MySQL的PHP代碼和MsSQL一起工作
我一直在努力工作,等待讓它工作,但我沒有真正得到任何地方。在本地機器上,我使用PHP 5.3.29和Sql Server 2012運行Apache服務器。
來自phpinfo()的信息; Apache的版本的Apache/2.2.25(的Win32)PHP/29年3月5日 我看不到MSSQL提及在phpinfo()函數,但在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);
我無法真正看到正在發生的事情,當我嘗試登錄我只是得到一個白色的屏幕,在控制檯中沒有任何信息或錯誤。
如果u得到一個白色的屏幕,這意味着您在您選擇PHP代碼的語法錯誤 – Somepub
你嘗試使用error_reporting(E_ALL); ? –
我試過error_reporting,但仍然沒有出現 – ArtleMaks