I have a simple php code which enters value into MySql database and then retrieves it and displays it. However when retrieving it always return null and Empty Set is echoed everytime. Can someone please help.

I am using WAMP Server.

Database name is trial and name of table is People. It has 2 attributes: name and email id

Following is my code:

$con=mysqli_connect("localhost","root","");

if (mysqli_connect_errno())

       echo "Failed to connect to MySQL: " . mysqli_connect_error();

mysqli_query($con,"INSERT INTO People VALUES ('xyz', 'abc@zzz.com')");

echo "Insertion Success";

$result = mysqli_query($con,"SELECT * FROM People");

if($result == null)

    echo "Empty Set";

else

   while($row = mysqli_fetch_array($result)) 
   {
       echo $row['name'] . " " . $row['emailid'];
       echo "<br>";
   }

   mysqli_close($con);
?>

Recommended Answers

All 2 Replies

After insertion of new value, close and then create new instance of query.

Member Avatar for diafol

You shouldn't have to do that. Is the insert successful? Change the values and check the db. Your echo doen't prove anything.

if($result == null)

is wrong anyway. $result will be FALSE not NULL on failure.

if($result)

may be better

ALso, I'd imagine that you need to specify the fields...

mysqli_query($con,"INSERT INTO People (field1, field2) VALUES ('xyz', 'abc@zzz.com')");

whatever they're called

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.