我試圖在成功登錄時向用戶顯示數據,但我的代碼不工作。顯示從mysqli到php的數據
以下是我的代碼:
if(isset($_POST['submit'])){
$username=$_POST['username'];
$password=$_POST['password'];
$query = mysqli_query($conn, "SELECT * FROM mylogin WHERE username='$username' and password='$password'");
if (mysqli_num_rows($query) != 0){
echo "sucess" , header('Location: welcome.php');
} else {
echo "invalid username or password";
}
}
?>
數據頁
$database_name = "id2425621_login";
$conn = mysqli_connect($servername, $username, $password, $database_name);
if (!$conn) {
die(mysqli_error($conn));
}
$sql = mysql_query("SELECT id, fname, lname, email FROM patients");
?>
<table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
<thead>
<tr>
<th>id</th>
<th>fname</th>
<th>lname</th>
<th>email</th>
</tr>
</thead>
<tbody>
<?php
while($row = mysql_fetch_assoc($sql)){
echo
"<tr>
<td>{$row['id']}</td>
<td>{$row['fname']}</td>
<td>{$row['lname']}</td>
<td>{$row['email']}</td>
</tr>\n";
}
?>
</tbody>
</table>
請指引我正確的語法來解決我的問題。
'mysql_fetch_assoc'應該是'mysqli_fetch_assoc'。你的代碼使用'mysql'和'mysqli'。你需要刪除'mysql' –
首先,初始sql語句容易受到sql注入攻擊,第二個是混合'mysqli'和'mysql' apis – RamRaider
您只需將mysql_fetch_assoc更改爲mysqli_fetch_assoc並且它可以工作。如果有其他問題,請告訴我。 –