2011-12-10 53 views
0
<?php 

function redirect_to_index_with_error(){ 
    echo '<meta http-equiv="refresh" content="0;url=index.php">'; 
} 

function go_to_home(){ 
    echo '<meta http-equiv="refresh" content="0;url=home.php">'; 
} 

$email = mysql_real_escape_string($_POST['username']); echo $email; 
$pwd = mysql_real_escape_string($_POST['password']); 
echo $pwd; 
$query = "SELECT * FROM users WHERE email='$email' AND password=MD5('$pwd')"; 
echo "query variable created."; 

mysql_connect("localhost","root","") or die(mysql_error()); 
echo "connected."; //nothing 

mysql_select_db("mcp") or die(mysql_error()); 

$results = mysql_query($query) or die(mysql_error()); 

if(mysql_num_rows($results) == 0){ 
    redirect_to_index_with_error(); 
    exit(); 
} 

$userID = null; 
$name = null; 
$school = null; 
$mod = null; 

while($user = mysql_fetch_array($results)){ 
    $userID = $user['ID']; 
    $name = $user['Name']; 
    $school = $user['School']; 
    if($user['Mod'] == '1') 
     $mod = true; 
    else 
     $mod = false; 

} 
if(!isset($_SESSION)) 
    session_start(); 

//set session variables 
$_SESSION["userID"] = $userID; 
$_SESSION["name"] = $name; 
$_SESSION["school"] = $school; 
$_SESSION["mod?"] = $mod; 

go_to_home(); 
exit(); 
?> 

PHP回聲一切,直到「連接」。它甚至不顯示mysql錯誤。我已經使用WAMP在Windows上完美無誤地運行了這些代碼,但在Mac上卻沒有使用MAMP。我已經驗證過服務器正在運行,所以我無法確定問題所在。我使用PHP 5.3.6。PHP似乎拒絕連接到MySQL

+0

如果一切順利,它似乎不會輸出任何東西,是嗎? –

+1

對不起,我忘了發佈整個代碼。我現在就這樣做。 – user1091707

+0

這段代碼應該在「連接」之後迴應什麼? –

回答

1

你的連接需要建立你叫mysql_real_escape_string()

之前那麼移動mysql_connect("localhost","root","") or die(mysql_error());頂端。

1

連接後,您必須調用mysql_real_escape_string()。
否則此函數返回一個空字符串,並且查詢失敗。

雖然它引發了一個錯誤,但它似乎你沒有看到。
所以,你應該打開顯示錯誤或偷看錯誤日誌 - 這是不可能的,如果沒有能力看到錯誤信息

此外,你必須改善你的形式邏輯。
爲了表明「PHP似乎拒絕連接到MySQL」,您必須先驗證它。連接只是一個單行並返回一個值。
你可以驗證這個值並做出一定的結論。
但是,運行幾行幾十行的整個代碼並且僅僅陳述一條就沒有意義了。

1

移動mysql_connect()聲明高於一切。

// put this at the TOP 
mysql_connect("localhost:3306","root","") or die(mysql_error()); 
1

正如其他人所提到的,看到這個提示: http://php.net/mysql_real_escape_string#refsect1-function.mysql-real-escape-string-notes

此外,你應該看到的錯誤,在發展中,至少。 參見:error_reporting()

+1

不僅在發展。 error_reporting應始終在最大 –

+0

正確的,我的意思是說,在一個現場,錯誤輸出應發送到日誌文件,而不是顯示在屏幕上,而在開發過程中,輸出是最容易查看時顯示在屏幕上(除非你有'tail -f/path/to/log'運行的終端:)) – Housni