I am going through an array of data with a foreach, but when going through it, it releases this Invalid argument supplied for foreach() and tells me that the variables are undefined. This is my code:
<?php
include 'Models.php';
class Restaurant extends Models{
private $datos;
public function __construct(){
$this->datos=array();
parent::__construct();
}
public function InfoRest(){
session_start();
$id = $_SESSION['usuario']['id_usuario'];
$sql = $this->pdo->prepare("SELECT * FROM restaurants WHERE id_usuario = '$id'");
$sql->execute();
if($sql->rowCount()>=1){
while($row=$sql->fetch()){
$this->datos[]=$row;
}
return $this->datos;
}else{
$sql->errorInfo()[2];
}
}
}
?>
$obj = new Restaurant();
$values = $obj->InfoRest();
foreach($values as $value){
}
<?php echo $value['name_rest'] ?>
Running it throws me those errors. I've given it a lot of thought but still can't figure it out. I have done var_dump in the execute and it releases true, and in the same project I have something done in the same way and it does not throw an error, for example this:
class Models{
protected $pdo;
private $datos;
public function __construct(){
$this->datos=array();
try{
$this->pdo = new
PDO('mysql:host=localhost;dbname=vmenu.us;charset=utf8','root','');
}
catch(PDOExcepcion $e)
{
echo $e->getMessage();
exit;
}
}
public function DetailsRestaurant($id){
$sql = $this->pdo->prepare("SELECT * FROM restaurants WHERE id_restaurant = :id");
$sql->bindParam(':id',$id);
$sql->execute();
if($sql->rowCount()>=1){
while($row = $sql->fetch()){
$this->datos[]=$row;
}
return $this->datos;
}else{
$sql->errorInfo()[2];
}
}
}
And then this in my View:
$obj = new Models();
$id = $_GET['id_restaurant'];
$details = $obj->DetailsRestaurant($id);
foreach($details as $result){
}
<?php echo $result['name_rest'] ?>
As you can see, it's the same
There is a statement error in your function. If you want to verify that the user exists, you don't need to use
numRows
, but can fill your array directly.Since in
else
it you can return the error message, so you can check if the array is empty in the destination.The function could be like this (I have shielded it against SQL injection).
And in the code to read it:
NOTE: In
InfoRest
I would implement another more logical way to return possible errors, which allows me to verify when receiving the data if there is any key with an error.In a future edition of the answer I might propose that solution if you're interested. I didn't do it here so as not to get too far away from your code getting into other stuff. The idea is to write code that is consistent.
If you are not interested in the error message in this case, you can do the following:
In this way, whether there is no data or if the query fails, it will return
NULL
and when receiving the data you only have to evaluate the variable:If you are interested in showing the user possible error messages from the query, then we will have three possible results:
A consistent way to program here would be to return in all cases an array that could be
NULL
, have the error message with a named key,error
or an array with our data.Since it seems to me here that the query will not return too many rows, I think we can use
fetchAll
for simplicity. Thus we bring all the results in a variable$resultado
. It can also be done as before, with thewhile
.The function would look like this:
And when you receive the data, you evaluate the three possible outcomes like so: