0

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);
?>
3
Contributors
2
Replies
16
Views
2 Years
Discussion Span
Last Post by diafol
0

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

Edited by diafol

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.