[英]can't retrieve data from database phpMyAdmin
我是一個學習如何設置數據庫和PHP腳本並按照示例進行操作的初學者,然后當我運行login.php腳本時,我無法從數據庫中檢索數據,我真的覺得這是一個非常簡單的問題其他人,但是我試圖解決它,但是沒有成功,所以有人可以看看我的代碼然后改正它嗎?
這是我的PHP腳本:
init.php:
<?php
$db_name = "webapp";
$mysql_username = "root";
$mysql_password = "";
$server_name = "localhost";
$con=mysqli_connect($server_name, $mysql_username, $mysql_password, $db_name);
if (!$con) {
echo "Connection Error ......." . mysqli_connect_error();
} else {
echo "<h3>Database connection Success .....</h3>";
}
?>
login.php:
<?php
require "init.php";
$user_name = "YASER";
$user_phone = "123456";
$sql_query = "select name from user_info where user_name like'$user_name'and
user_phone like'$user_phone';";
$result = mysqli_query($con,$sql_query);
if (mysqli_num_rows($result)>0)
{
$row = mysqli_fetch_assoc($result);
$name = $row["name"];
echo "<h3> Hello And Wellcome" . $name . "</h3>";
} else {
echo " No Info Is Available .......";
}
?>
1st:首先檢查查詢是否正在executing
或failing
if(!$result){ echo mysqli_error($con); }
第二:用=
代替like
$sql_query = "select name from user_info
where user_name='$user_name' and
user_phone='$user_phone'";
第三:您需要在查詢中提供適當的間距
like'$user_name'and
^^^ ^^^
至
like '$user_name' and
您的查詢中有錯誤。
試試這個發現錯誤
$result = mysqli_query($con,$sql_query) or die(mysqli_error($con));
您的查詢應如下所示...
'SELECT name FROM user_info WHERE user_name LIKE "'.$user_name.'" AND user_phone LIKE "'.$user_phone.'"';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.