Reputation: 19
So I'm trying to call an array which is generated from a database, here is the function in the class file:
public function select($array)
{
try
{
$result ="SELECT id,make,model FROM cars";
$stmt = $this->db->query($result);
$array = $stmt->fetchAll();
}
catch (PDOException $e)
{
echo $e->getMessage();
}
}
and here i'm trying to call the array, at least I am, quite new to this.
include ('CarsDb.class.php');
$db = new CarsDb();
$db->connect();
$carArray = $db->select($array);
var_dump($carArray);
Now the var_dump just shows NULL, however if I put a var_dump in the function it'll show the array on my page, so can anyone help me?
Upvotes: 0
Views: 127
Reputation: 4142
Return array from the select function
public function select($array=array()) {
try {
$result ="SELECT id,make,model FROM cars";
$stmt = $this->db->query($result);
$array = $stmt->fetchAll();
} catch (PDOException $e) {
echo $e->getMessage();
}
return $array;
}
Upvotes: 0
Reputation: 5651
You need to return a value from your function. Do this.
public function select($array) {
try {
$result ="SELECT id,make,model FROM cars";
$stmt = $this->db->query($result);
return $stmt->fetchAll(); //Return value here
} catch (PDOException $e) {
echo $e->getMessage();
}
}
And now this should work.
include ('CarsDb.class.php');
$db = new CarsDb();
$db->connect();
$carArray = $db->select($array);
Upvotes: 3