Welcome to WebmasterWorld Guest from 54.91.71.108

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

MySql Select Command returning null

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

New User from BD 

joined:Sept 27, 2014
posts:4
votes: 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);
?>
1:23 pm on Sept 28, 2014 (gmt 0)

Senior Member from GB 

WebmasterWorld Senior Member brotherhood_of_lan is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Jan 30, 2002
posts:4998
votes: 47


Try changing this line to see whether the insert worked

mysqli_query($con,"INSERT INTO People VALUES ('xyz', 'abc@zzz.com')") 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.
5:59 am on Sept 29, 2014 (gmt 0)

Senior Member

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

joined:Oct 15, 2004
posts:942
votes: 0


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(); }
9:26 am on Sept 30, 2014 (gmt 0)

New User

joined:July 6, 2014
posts: 32
votes: 0


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

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members