每當我嘗試登錄我的網站上它總是給出: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");
?>
更改'$ connection = mysql_connect($ host,$ user,$ pass);'到'$ conn = mysql_connect($ host,$ user,$ pass);' –