0

I have a PHP query for select data from database its working. and I'm user foreach for print data out of while loop. And it's also working but when I'm trying to select same value with foreach it's showing error. I check my code and i right.

Code id Here

<?php
$sql = "SELECT *  FROM user WHERE userid = '205'";
$result = mysqli_query($con_db, $sql);
//Creating array 
$userdata = array();

if (mysqli_num_rows($sql) > 0) {
    while($row = mysqli_fetch_assoc($sql)) {
        $userdata[] =  $row;
    }
}else{
    echo "No Result Found";
}

//Here I'm Showing Data
foreach($userdata as $user) {
    echo $user["useremail"];
}
?>

When i'm Run This Code like

<?php
foreach($userdata as $user) {
    echo "<p>".$user['userloginemail']."</p>";
}
echo "<br><br>";
foreach($userdata as $userdata) {
    echo "<p>".$user['userloginemail']."</p>";
}
?>

This is Result

example@gmail.com

//This is Error
Notice:  Undefined variable: user in 
C:\xampp\htdocs\ats\recruiters\recruiter.php on line 111

Warning:  Invalid argument supplied for foreach() in 
C:\xampp\htdocs\ats\recruiters\recruiter.php on line 111
Azeez Kallayi
  • 2,567
  • 1
  • 15
  • 19

5 Answers5

0

$sqldata not declared!

Try this

<?php
foreach($userdata as $user) {
    echo "<p>".$user['userloginemail']."</p>";
}
echo "<br><br>";
foreach($userdata as $user) {
    echo "<p>".$user['userloginemail']."</p>";
}
?>
6339
  • 475
  • 3
  • 16
  • @Deepak try this foreach($userdata as $user) { echo "

    ".$user['userloginemail']."

    "; } instead of foreach($userdata as $userdata) { echo "

    ".$user['userloginemail']."

    "; }
    – 6339 Jan 31 '18 at 06:26
  • @Deepak you are using foreach($userdata as $userdata) but you need to change the variable to foreach($userdata as $user). Checkout my answer. – 6339 Jan 31 '18 at 06:32
0

This is not sollution but suggestion that this is silly mistakes that developers do and makes bigger problem So it is good if you learn own self.

Notice: Undefined variable: sqldata in

This error mean by That The Defined variable by you is not defined or not a valid . So First Learn this error HERE

And

Warning: Invalid argument supplied for foreach() in

This mean by The Value Supplied in foreach is not valid as per foreach requirement

So kindly check Whetther array is Valid or not by print_d()

TarangP
  • 2,711
  • 5
  • 20
  • 41
0

Because there is no array or variable defined as $sqldata

I think you want this

<?php
 foreach($userdata as $sqldata) {
    echo "<p>".$sqldata['userloginemail']."</p>";
}
echo "<br><br>";
foreach($userdata as $sqldata) {
    echo "<p>".$sqldata['userloginemail']."</p>";
}
?>
Sanjit Bhardwaj
  • 893
  • 7
  • 13
0

The issue is that you are not passing $result to fetch the data. $sql is just the query string not the result that comes out from the query Do this instead

if (mysqli_num_rows($result) > 0) {//pass $result
    while($row = mysqli_fetch_assoc($result)) {//pass $result
        $userdata[] =  $row;
    }
} else {
    echo "No Result Found";
}

Also make sure to check if array is not empty. Use empty()

if(!empty($userdata)){
  //then loop
}
Rotimi
  • 4,783
  • 4
  • 18
  • 27
0

Check the second foreach loop you are declaring $userdata and accessing with $user.

Your code:

foreach($userdata as $userdata) {
    echo "<p>".$user['userloginemail']."</p>";
}

Correct Code:

foreach($userdata as $user1) {
    echo "<p>".$user1['userloginemail']."</p>";
}
Muhammad Tarique
  • 1,407
  • 1
  • 13
  • 17