我创建了一个简单的PHP页面,该页面从POST请求中检索数据并将其保存在MYSQL数据库中。数据以正确的方式插入。现在,我想执行一个Select查询以从表中检索数据并将其显示在UI表中。问题在于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>
我要去哪里错了?如何获得所需的输出?
您的选择查询在列和表名两边加上单引号。您应该删除它们,或将其替换为反引号:
"SELECT `employee_id` , `employee_name` , `employee_email` , `employee_mobile` , 'employee_address'
FROM `employee_details` "
更新:感谢@bartfriederichs指出select
子句中带单引号的引用元素不是无效的,因为查询将返回传递的相同数据。用单引号引用表名是无效的。
我的答案假设您employee_id, employee_name, employee_email, employee_mobile, employee_address
要从table的列中获取数据employee_details
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句