2014-01-29 313 views
0

我創建了一個簡單的PHP頁面,它從POST請求中檢索數據並將其保存在MYSQL數據庫中。數據以正確的方式插入。現在,我想執行Select查詢來從表中檢索數據並將其顯示在UI表中。 問題是Select語句給出了一個錯誤MySql Select查詢返回錯誤結果

這是錯誤

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\abhideep_test_project\welcome.php on line 48 

這是PHP代碼

<html> 
<body> 

<?php $name = $_POST["name"]; 
$email = $_POST["email"]; 
$mobile = $_POST["mobile"]; 
$address = $_POST["address"]; 
//=============Data Insertion================= 

// Create connection 
$con=mysqli_connect("localhost","root","","employee_db"); 

// Check connection 
if (mysqli_connect_errno()) 
    { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 
/*mysqli_query($con,"INSERT INTO employee_details (employee_name, employee_email, employee_mobile, employee_address) 
VALUES ('Glenn', '[email protected]','9830098300','2/A, Work Lane')");*/ 

mysqli_query($con,"INSERT INTO employee_details (employee_name, employee_email, employee_mobile, employee_address) 
VALUES ('$name','$email','$mobile','$address')"); 

mysqli_close($con); 
//=============Data Insertion================= 


//=============Data Display================= 
$con=mysqli_connect("localhost","root","","employee_db"); 
// Check connection 
if (mysqli_connect_errno()) 
    { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 


$result = mysqli_query($con,"SELECT 'employee_id' , 'employee_name' , 'employee_email' , 'employee_mobile' , 'employee_address' 
FROM 'employee_details' "); 

echo "<table border='1'> 
<tr> 
<th>Name</th> 
<th>Email</th> 
<th>Mobile</th> 
<th>Address</th> 
</tr>"; 

while($row = mysqli_fetch_array($result)) 
    { 
    echo "<tr>"; 
    echo "<td>" . $row['employee_name'] . "</td>"; 
    echo "<td>" . $row['employee_email'] . "</td>"; 
    echo "<td>" . $row['employee_mobile'] . "</td>"; 
    echo "<td>" . $row['employee_address'] . "</td>"; 
    echo "</tr>"; 
    } 
echo "</table>"; 

mysqli_close($con); 
//=============Data Display================= 

?> 
</body> 
</html> 

我要去哪裏錯了?可以做些什麼來獲得理想的輸出?

回答

2

您選擇的查詢有單引號圍繞列和表名稱。您應該刪除它們,或用反引號替換它們:

"SELECT `employee_id` , `employee_name` , `employee_email` , `employee_mobile` , 'employee_address' 
FROM `employee_details` " 

更新:感謝@bartfriederichs的指出引用與單引號select條款內容是不是無效的,因爲查詢將返回相同的數據通過。但引用帶單引號的表名是無效的。

我的答案假定您需要表employee_details的列employee_id, employee_name, employee_email, employee_mobile, employee_address中的數據。

+0

列周圍是不是無效的,它只是返回的值。桌子周圍是。 –

+0

@BartFriederichs,是的,謝謝你抓到這句話。更新答案刪除不正確的語句 – vee

2

這是不正確

$result = mysqli_query($con,"SELECT 'employee_id' , 'employee_name' , 'employee_email' , 'employee_mobile' , 'employee_address' 
FROM 'employee_details' "); 

應該

$result = mysqli_query($con,"SELECT `employee_id` , `employee_name` , `employee_email` , `employee_mobile` , `employee_address` 
FROM `employee_details` "); 
0

你的SELECT語句應該像this..Use backtick列名

$result = mysqli_query($con,"SELECT `employee_id` , `employee_name` , `employee_email` , `employee_mobile` , `employee_address` 
FROM `employee_details` "); 
0

嘿,你需要使用addslashes在你的插入變量中起作用,如

$name = addslashes($_POST["name"]); 
$email = addslashes($_POST["email"]); 
$mobile = addslashes($_POST["mobile"]); 
$address = addslashes($_POST["address"]); 

然後運行查詢,

mysqli_query($con,"INSERT INTO employee_details (employee_name, employee_email, employee_mobile, employee_address) 
VALUES ('$name','$email','$mobile','$address')"); 
+0

添加addslashes()的壞主意。爲什麼不使用mysqli_real_escape_string() –

+0

嘿,這不是一個壞主意。我們應該在服務器端處理這些類型的東西,而不是在數據庫中。 – Shivam

0
<html> 
<body> 
<?php 
$con = mysql_connect("localhost","root","") or die("mysql error"); 
     $db = mysql_select_db("employee_db",$con) or die ("problem in connecting database"); 
     ?> 
<?php 

$name = $_POST["name"]; 
$email = $_POST["email"]; 
$mobile = $_POST["mobile"]; 
$address = $_POST["address"]; 


$query =mysqli_query("INSERT INTO employee_details (employee_name, employee_email, employee_mobile, employee_address) 
VALUES ('$name','$email','$mobile','$address')"); 

?> 
<table> 
<thead> 
<tr> 
<th>Name</th> 
<th>Email</th> 
<th>Mobile</th> 
<th>Address</th> 
</tr> 
</thead> 

<tbody> 

<?php 
$sql ="SELECT 'employee_id' , 'employee_name' , 'employee_email' , 'employee_mobile' 'employee_address' 
FROM 'employee_details' "; 
       $runquery=mysql_query($sql); 
            $rows= mysql_num_rows($runquery); 

            if(is_resource($runquery) && $rows>0)  
         { 
            while($result= mysql_fetch_array($runquery)) 
          { 


            echo ' <tr>'; 
        echo '<td>'.$result['employee_name'].'</td>'; 
        echo '<td>'. $result['employee_email'].'</td>'; 
        echo '<td>'.$result['employee_mobile'].'</td>'; 
        echo '<td>'.$result['employee_address'].'</td></tr>'; 

          }} 
      ?>    
        </tbody> 
       </table> 

<?php mysqli_close($con);?> 
<!-- =============Data Display=================--> 

</body> 
</html>