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:
echo "Failed to connect to MySQL: " . mysqli_connect_error();
mysqli_query($con,"INSERT INTO People VALUES ('xyz', 'email@example.com')");
echo "Insertion Success";
$result = mysqli_query($con,"SELECT * FROM People");
if($result == null)
echo "Empty Set";
while($row = mysqli_fetch_array($result))
echo $row['name'] . " " . $row['emailid'];
I believe it’s because you aren’t selecting the database to use when you’re connecting. In the procedural interface of MySQLi, there’s two ways to do this:
// put the DB name as the fourth argument of mysqli_connect()
$con = mysqli_connect('host', 'username', 'pws', 'dbname');
// use the mysqli_select_db() function
$con = mysqli_connect('host', 'username', 'pas');
The former way is more terse (and so it is usually preferred), whereas the latter way is more like the original MySQL extension (though with the two parameters for
While i like the positive statements (“Insertion Success”) You may also want to consider actually checking that the query succeeded. (If the Insert query returns FALSE, there was an error in it, which should be checked with mysqli_error)
Thanks tpunt. It’s working.