简体   繁体   中英

MySql Select query returning erroneous results

I have created a simple PHP page which retrieves data from a POST request and saves it in MYSQL database. The data is getting inserted in the proper way. Now, I want to execute a Select query to retrieve the data from the table and show it in a UI Table. The problem is that the Select statement is giving an error .

This is the error :

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

This is the PHP code :

<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>

Where am I going wrong? What can be done to get the desired output?

Your select query has single quotes around columns and table name. You should remove them, or replace them with a backtick:

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

Update: Thanks to @bartfriederichs for pointing out that quoting elements in select clause with single quote is not invalid as the query would return the same data passed. Quoting table name with single quote however is invalid.

My answer assumes that you want data in columns employee_id, employee_name, employee_email, employee_mobile, employee_address from table employee_details .

This is not correct

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

should be

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

Your select statement should be like this..Use backtick for column name

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

Hey you need to use addslashes function in your insert variables like this,

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

then run your query,

mysqli_query($con,"INSERT INTO employee_details (employee_name, employee_email, employee_mobile, employee_address) 
VALUES ('$name','$email','$mobile','$address')");
<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM