2017-08-09 95 views
0

我是一名初學者,學習如何設置數據庫& PHP腳本並遵循示例 來做到這一點,那麼當我運行login.php腳本時,我無法從數據庫中檢索數據數據庫, 我真的覺得這是一個非常簡單的問題,但我試圖解決它但沒有成功,所以有人可以看看我的代碼,然後糾正它?無法從數據庫中檢索數據phpMyAdmin

這裏是我的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 ......."; 
    } 
?> 

this is the result

+0

你的代碼是相當混亂 - 嘗試刪除從SQL語句中的分號,加之間的一些空間字,再試一次廣告回來,進度 –

+1

你應該使用'='而不是'like' –

+1

*第一:*首先檢查查詢是否正在執行或失敗if(!$ result){echo mysqli_error($ con); }'* 2nd:*使用'='而不是'like' – JYoThI

回答

0

1:首先檢查查詢executing或第二

if(!$result){ echo mysqli_error($con); } 

:代替使用=like

$sql_query = "select name from user_info 
       where user_name='$user_name' and 
       user_phone='$user_phone'"; 

3:你需要給適當間距在查詢

like'$user_name'and 
    ^^^  ^^^ 

like '$user_name' and 
0

您的查詢中有錯誤。

試試這個發現錯誤

$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.'"';