2017-09-06 122 views
0

美好的一天。我試圖做一個登錄表單使用PHP和XAMPP運行代碼。我已經在phpMyAdmin有一個數據庫,我得到這個錯誤登錄形式錯誤。調用未定義函數mysql_query()

連接!

致命錯誤:未捕獲錯誤:調用未定義的函數mysql_query()在C:\ xampp \ htdocs \ login \ connection.php中:34堆棧跟蹤:#0 {main}拋出C:\ xampp \ htdocs \ login \在線connection.php 34

每次我執行此代碼

<?php 
$host = 'localhost'; 
$uname = 'root'; 
$pword = ''; 
$db = 'login'; 
$port = '3306'; 

$link = mysqli_init(); 
$success = mysqli_real_connect(
     $link, 
     $host, 
     $uname, 
     $pword, 
     $db, 
     $port 
    ); 


if(isset($_POST['user'])) 
{ 
    $username = $_POST['user']; 
} 
if(isset($_POST['pass'])) 
{ 
    $password = $_POST['pass']; 
} 



$con = new mysqli($host, $uname, $pword, $db) or die("Connection failed". mysqli_error()); 

echo ("Connected!"); 

$result = mysql_query($success. "select * from db_login where username = $username and password = $password"); 

$row = mysql_fetch_array($result); 

if($row['username'] == $username && $row['password'] == $password) 
{ 
    echo("Welcome!". $row['username']); 

} 
else 
{ 
    echo("Login failed"); 

} 


?> 

我試圖在YouTube上搜索如何修正這個錯誤,但他們只告訴我的PHP的版本是過時的/更新。我很困惑。

+0

不要叫'mysql_'功能,當你用'mysqli'的[我可以在PHP MySQL的混合API的 – Scuzzy

+0

可能的複製?](https://stackoverflow.com/questions/17498216/can-i-mix-mysql-apis-in-php) – Scuzzy

+0

它*是*過時。 'mysqli'取代了'mysql'。出於你的目的,你可以做一個搜索和替換。 – Sablefoste

回答

0

如果已經設置爲你的數據庫的正確連接....

$host = 'localhost'; 
$uname = 'root'; 
$pword = ''; 
$db = 'login'; 
$success = mysqli_connect($host,$uname,$pword,$db); 
// Check connection 
if (mysqli_connect_errno()) 
    { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 


if(isset($_POST['user'])) 
{ 
    $Username = $_POST['user']; 
} 
if(isset($_POST['pass'])) 
{ 
    $Password = $_POST['pass']; 
} 
// removes backslashes 
$username = stripslashes($Username); 
//escapes special characters in a string 
$username = mysqli_real_escape_string($success,$username); 
$password = stripslashes($Password); 
$password = mysqli_real_escape_string($success,$password); 

$result = mysqli_query($success. "select * from db_login where username = $username and password = $password") or die(mysql_error()); 
$row = mysqli_num_rows($result); 

    if($rows==1){ 
     echo("Welcome!". $row['username']); 

    } 
    else 
    { 
     echo("Login failed"); 

    } 
相關問題