0

The suggested questions have not quite covered my issue. I'm intrigued by the answer here, Invalid argument supplied for foreach! but don't quite see how it applies in my case.

I have the following code, which does work as I would expect; the code builds an array of email addresses by matching an id given as hidden form posted from the previous page, finding the 'email' column in a mysql database.

However, I also get the error: Invalid argument supplied for foreach() on line 50

Any help appreciated.

function findEmail($id) {
$mysqli = new mysqli(DBHOST,DBUSER,DBPASS,DB);
if ($mysqli->connect_errno) {
    error_log("Cannot connect to MySQL: " . $mysqli->connect_error);
    return false;
}

$query = "SELECT email FROM `Students` WHERE id = '$id'";
$result = $mysqli->query($query);
while($row = $result->fetch_array()) {
    $rows[] = $row;
}
$email = "";
foreach($rows as $row) {      //This is line 50
    $email = ($row['email']);
}
return $email;
}

$userId = array();
$userEmail = array();

foreach($_POST as $key => $value) {
array_push($userId, $value);
array_push($userEmail, findEmail($value));
}
Community
  • 1
  • 1
Russell
  • 655
  • 2
  • 9
  • 21
  • 1
    you loop twice onece in the while then the foreach, its pointless –  Apr 16 '14 at 21:21
  • You also seem to just assign `$email` in that `foreach` loop, so at the end you are not adding to a variable but over-riding it every time. – ek9 Apr 16 '14 at 21:29
  • The id is unique so there should only ever be one email address. – Russell Apr 16 '14 at 21:37

5 Answers5

0

The error isn't fatal, you can make it be... however the error is that $rows is not defined -- your query must be returning empty.

awm
  • 2,723
  • 2
  • 18
  • 26
0

You might have an element in the $rows array that is NULL, if you var_dump($rows) do you have any NULLS?

rmcfrazier
  • 444
  • 4
  • 8
  • Yes - in fact despite only having hidden elements in the form the value "Submit" is included in $_POST, which is causing the error. – Russell Apr 16 '14 at 22:07
0

You need to initialize the vaiable $rows, else as stated on http://www.php.net/manual/en/control-structures.foreach.php the foreach "will issue an error when you try to use it on a variable with a different data type or an uninitialized variable"

So before line 50 put this:

$rows=null;
0

the function could be optermised like so:

function findEmail($id) {
$mysqli = new mysqli(DBHOST,DBUSER,DBPASS,DB);
if ($mysqli->connect_errno) {
    error_log("Cannot connect to MySQL: " . $mysqli->connect_error);
    return false;
}

$query = "SELECT email FROM `Students` WHERE id = '$id'";
$result = $mysqli->query($query);  

  $row = $result->fetch_array($query);

  return $row[0];
}
0

The value of "submit" was being sent to the function. Adding the second line below solved the problem.

foreach($_POST as $key => $value) {
    if($key == "submit") continue;
    array_push($userId, $value);
    array_push($userEmail, findEmail($value));
}
Russell
  • 655
  • 2
  • 9
  • 21