Forum Moderators: coopster

Message Too Old, No Replies

MySql Select Command returning null

         

shammi007

12:07 pm on Sep 28, 2014 (gmt 0)

10+ Year Member



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', '[email protected]')");

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);
?>

brotherhood of LAN

1:23 pm on Sep 28, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Try changing this line to see whether the insert worked

mysqli_query($con,"INSERT INTO People VALUES ('xyz', '[email protected]')") or die(mysqli_error($con));

The query looks fine but since you didn't specifiy the fields being inserted, it may be that there are more columns in the table than the two you're inserting.

omoutop

5:59 am on Sep 29, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The error is here:
$con=mysqli_connect("localhost","root","");


Mysqli connection expects 4 parameters (host, username, password, database name)

Change your code to something like:
$con = mysqli_connect("localhost", "root", "", "db_name_here");
if (!$con ) { echo 'Connect Error ('.mysqli_connect_errno().')'. mysqli_connect_error(); exit(); }

Dammy

9:26 am on Sep 30, 2014 (gmt 0)

10+ Year Member



@omoutop
yap, he need to specify the db name he's working with b4 it will work.
u cn check ur phpMyadmin 4 confirmation.