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