2014-10-01 22 views
0

我正在寫一個簡單的程序在PHP中,要求用戶登錄。我有一個工作代碼,但每當進入一個錯誤的用戶名或密碼,我得到了,說以下內容的異常:登錄腳本在PHP中我的MYSQL顯示已棄用警告

推薦使用:mysql_connect()函數:mysql擴展已被棄用,並將在將來被移除:使用mysqli或PDO,而不是在C:\ wamp \ www \ Directory \ login.php在線67

我有的腳本運行良好,但我只需要擺脫此警告消息。 以下是我的php代碼。

<?php 
$username = $_POST['username']; 
$password = $_POST['password']; 

// Connect to the database 
$con = mysql_connect('localhost', 'root', ''); 

// Make sure we connected succesfully 
if (!$con) { 
    die('Connection Failed' . mysql_error()); 
} 
// Select the database to use 
mysql_select_db("Garden", $con); 

$q = mysql_query("select * from register where username='" . $username . "' and password='" . $password . "' ") or die(mysql_error()); 
$res = mysql_fetch_row($q); 
if ($res) { 
    header('location:home.php'); 
} else { 
    echo 'Error. The Username or Password that you entered is invalid.'; 
} 
?> 

我不知道如果即時通訊使用的東西。這是我第一次使用php。拜託我需要你的幫忙。謝謝。

+1

請勿使用折舊功能,請使用mysqli - http://php.net/manual/en/book.mysqli.php或PDO - http://php.net/manual/en/book.pdo .php – andyroo 2014-10-01 10:53:50

回答

1

下面是使用庫MySQLi相同的代碼:

<?php 
$mysqli = new mysqli('localhost', 'user', 'password', 'db_name'); 
if(mysqli_connect_errno()){ 
    printf("DB Connect failed: %s\n", mysqli_connect_error()); 
    exit(); 
} 
// Add the UTF8 Support 
$mysqli->query("SET NAMES 'utf8'"); 
$mysqli->query("SET CHARACTER SET utf8"); 

$username = isset($_POST['username']) ? $_POST['username'] : ''; 
$password = isset($_POST['password']) ? $_POST['password'] : ''; 
$username = $mysqli->real_escape_string($username); 
$password = $mysqli->real_escape_string($password); 
$query = "SELECT * FROM `register` WHERE username='" . $username . "' and password='" . $password . "' LIMIT 1"; 
$result = $mysqli->query($query); 
if(!empty($result)){ 
    if($result->num_rows == 1) { 
     header("Location: home.php"); 
    } else { 
     echo "Error. The Username or Password that you entered is invalid."; 
    } 
} 
mysqli_close($mysqli); 
$mysqli = null; 
?> 

你不應該避免PHP錯誤。您應該始終相應地修復代碼。 您可以使用PDO或MySQLi。

+0

感謝人......工作。非常感謝 – 2014-10-01 11:10:39

+0

@HarunaAdo我很高興它的工作:)你能否請把這個答案標記爲有效,以便其他人找到正確的答案? – 2014-10-01 11:32:35

0

使用mysqli重寫您的代碼(很小的努力),即使它正常工作並檢查消息是否仍然出現。