2013-12-21 38 views
-4

每當我嘗試登錄我的網站上它總是給出:PHP MYSQL連接變量不存在?當它

注意:未定義的變量:P中CONN:\ XAMPP \ htdocs中\ CraftedLogin \上線authenticate.php 13

警告:請求mysql_query()預計參數2是資源,NULL給出 P中:\ XAMPP \ htdocs中\ CraftedLogin \ authenticate.php上線13

警告:mysql_num_rows()預計參數1是資源,空 給出在第16行的P:\ xampp \ htdocs \ CraftedLogin \ authenticate.php中 錯誤的用戶名或密碼!無法登錄,GFG

這是我的代碼:

<?php 

$username = $_POST['username']; 
$password = $_POST['password']; 
include("dbConfig.php"); 

$sql = "SELECT * FROM `users` WHERE `username` = '$username' AND `password` = '$password'"; 

$rs = mysql_query($sql,$conn); 

$num = mysql_num_rows($rs); 

if($num != 0) 
{ 
    $msg = "<h3>Welcome $username - your log-in succeeded!</h3>"; 
} 
else 
{ 
    $msg = "Wrong username or password! Cannot login as $username"; 
} 
?> 

<html> 

<head> 
    <title>Log-In</title> 
    </head> 

    <body> 
    <?php echo($msg); ?> 
    </body> 

</html> 

而且,這裏是我的dbConfig.php:

<? 
$host = "localhost"; 
$user = "CraftedLogin"; 
$pass = "craftedlogin"; 
$db = "craftedlogin"; 
$self =  $_SERVER['PHP_SELF']; 
$referer = $_SERVER['HTTP_REFERER']; 
$connection = mysql_connect($host, $user, $pass); 
if (!$conn) 
{ 
echo "Error connecting to database.\n"; 
} 
$rs = @mysql_select_db($db, $conn) 
     or die("Could not select database"); 
?> 

誰能幫助我?

注意:我已經嘗試將$ connection更改爲$ conn,但這次仍然會給出$ num變量的錯誤,但我認爲我可以很快對此進行排序。

NEW CODE:

<?php 

$username = $_POST['username']; 
$password = $_POST['password']; 
include("dbConfig.php"); 

$sql = "SELECT * FROM `users` WHERE `username` = '$username' AND `password` = '$password'"; 

$rs = @mysql_select_db($db, $conn); 

$numr = mysql_num_rows($rs); 

if($numr != 0) 
{ 
    $msg = "<h3>Welcome $username - your log-in succeeded!</h3>"; 
} 
else 
{ 
    $msg = "Wrong username or password! Cannot login as $username"; 
} 
?> 

<html> 

<head> 
    <title>Log-In</title> 
    </head> 

    <body> 
    <?php echo($msg); ?> 
    </body> 

</html> 

和dbConfig.php:

<? 
$host = "localhost"; 
$user = "CraftedLogin"; 
$pass = "craftedlogin"; 
$db = "craftedlogin"; 
$self =  $_SERVER['PHP_SELF']; 
$referer = $_SERVER['HTTP_REFERER']; 
$conn = mysql_connect($host, $user, $pass); 
if (!$conn) 
{ 
echo "Error connecting to database.\n"; 
} 
$rs = @mysql_select_db($db, $conn) 
     or die("Could not select database"); 
?> 
+0

更改'$ connection = mysql_connect($ host,$ user,$ pass);'到'$ conn = mysql_connect($ host,$ user,$ pass);' –

回答

0

變化

$connection = mysql_connect($host, $user, $pass); 

$conn = mysql_connect($host, $user, $pass); 

這就是爲什麼它不工作。您正在使用錯誤的變量名稱。

編輯: 那很可能是你想要什麼:

$sql = "SELECT * FROM `users` WHERE `username` = '$username' AND `password` = '$password'"; 

$result = mysql_query($sql, $conn); 

$numr = mysql_num_rows($result); 
+0

嘗試仍然不起作用 – MCRocks999

+0

是錯誤,你收到與你的問題相同? – aksu

+0

編號它給出了:警告:mysql_num_rows()期望參數1是資源,null在第11行P:\ xampp \ htdocs \ CraftedLogin \ authenticate.php中給出在上面的問題中查看新代碼 – MCRocks999

0

你的連接是$connection當你使用$conn其他任何地方。

0

在這段代碼:

$rs = @mysql_select_db($db, $conn) or die("Could not select database"); 

你傳入$康恩,但你的連接被定義爲$連接。將其更改爲:

$rs = @mysql_select_db($db, $connection) or die("Could not select database"); 

將解決此問題。

0

修正:

$conn = mysql_connect($host, $user, $pass); 
0
$connection = mysql_connect($host, $user, $pass); 

變化到

$康恩=的mysql_connect($主機,$用戶,$通);