2016-06-16 165 views
1

我想爲我的服務器創建一個登錄系統。我使用xampp爲本地網站使用了類似的代碼,但現在我正在嘗試使用外部服務器。我得到的錯誤是這個。登錄系統的sqlserver連接錯誤

Array([0] => Array([0] => 42000 [SQLSTATE] => 42000 [1] => 102 [code] => 102 [2] => [ODBC] 11爲SQL Server] [SQL服務器]附近有語法錯誤'. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Incorrect syntax near ''))

,我的代碼如下:

<?php 

session_start(); 

$inputuser = $_REQUEST['Username']; 
$inputpass = $_REQUEST['Password']; 

$Server = "MyServer"; 
$user = "user"; 
$password = "pass"; 
$database = "mydb"; 
$table = "users"; 

$connectionInfo = array("Database"=>$database,"UID"=>$user, "PWD"=>$password); 

$link = sqlsrv_connect($Server, $connectionInfo); 

if ($link === false) { 
    echo "Connection failed. \n"; 
    die(print_r(sqlsrv_errors(), true)); 
} 


$query = "SELECT `Username` FROM `onlinereporting` . `users` WHERE `Username` = '$inputuser'"; 
$querypass = "SELECT `Password` FROM `onlinereporting` . `users` WHERE `Password` ='$inputpass'"; 

$result = sqlsrv_query($link,$query); 
$resultpass = sqlsrv_query($link,$querypass); 

if ($result === false) { 
    die(print_r(sqlsrv_errors(), true)); 
} 

$row = sqlsrv_fetch_array($result); 
$rowpass = sqlsrv_fetch_array($resultpass); 

$serveruser = $row["Username"]; 
$serverpass = $rowpass["Password"]; 


if ($serveruser And $serverpass) { 

    header('Location: index.html'); 

} 

else { 

    header('Location: FailedPage.html'); 

} 
?> 

連接工作正常,當我嘗試發生的唯一問題檢查結果是否爲假。

當我不試圖檢查$結果時,我插入的所有內容都被視爲錯誤的用戶名和密碼。

歡迎任何幫助或建議。

+0

現在誤差改變,它看起來像這樣: 陣列([0] =>數組([0] => 42000 [SQLSTATE] => 42000 [ 1] => 102 [code] => 102 [2] => [Microsoft] [用於SQL Server的ODBC驅動程序11] [SQL Server] [用戶]附近的語法錯誤[message] => [Microsoft]對於SQL Server] [SQL Server]'users'附近的語法不正確。)) –

回答

2

我相信原因是您使用反引號來引用表和列名,而在SQL服務器名稱中引用了"。嘗試改變反引號到雙引號,即:

$query = "SELECT \"Username\" FROM \"onlinereporting\" . \"users\" WHERE \"Username\" = '$inputuser'";