我创建了一个简单的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', 'glenn@gmail.com','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>
我哪里错了?可以做些什么来获得所需的输出?
答案 0 :(得分:2)
您的选择查询在列和表名称周围有单引号。你应该删除它们,或者用反引号替换它们:
"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
中的列employee_details
中的数据。
答案 1 :(得分: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` ");
答案 2 :(得分:0)
您的select语句应如下所示。使用backtick
作为列名
$result = mysqli_query($con,"SELECT `employee_id` , `employee_name` , `employee_email` , `employee_mobile` , `employee_address`
FROM `employee_details` ");
答案 3 :(得分: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')");
答案 4 :(得分: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>