scripter50
IS-IT--Management
I'm have trouble getting values from an array returned from a php class. Code example:
<?php
require_once("include/NameDetails.php");
class TestQueries {
var $CE_Config_DB = NULL;
function getNameInfo($searchType, $searchValue) {
$resultArray = array();
$query = "SELECT * ";
$query .= "FROM table ";
if ($result = $this->CE_Config_DB->query($query)) {
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$nameDetails = new NameDetails();
$nameDetails->firstName = $row['firsName'];
$nameDetails->lastName = $row['lastName'];
$nameDetails->age = $row['age'];
$resultArray[] = $nameDetails;
}
}
}
return $resultArray;
}
}
?>
# call to the class:
<?php
include("include/TestQueries.php");
$testQueries = new TestQueries();
$nameData = $bvoipQueries->getNameInfo("name", "Fred");
# the array that is returned looks like this:
Array
(
[0] => NameDetails Object
(
[firstName] => Frank
[lastName] => Smith
[age] => 62
)
)
My issue is how to get the values out of the array??? It's the object that is throwing me?
If I try:
if(is_array($nameData)) {
foreach($nameData as $key=>$value) {
if($key == "firstName") {
$firstName = $value;
echo $firstName;
}
}
}
The error I get is:
"Catchable fatal error: Object of class NameDetails could not be converted to string in .....
<?php
require_once("include/NameDetails.php");
class TestQueries {
var $CE_Config_DB = NULL;
function getNameInfo($searchType, $searchValue) {
$resultArray = array();
$query = "SELECT * ";
$query .= "FROM table ";
if ($result = $this->CE_Config_DB->query($query)) {
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$nameDetails = new NameDetails();
$nameDetails->firstName = $row['firsName'];
$nameDetails->lastName = $row['lastName'];
$nameDetails->age = $row['age'];
$resultArray[] = $nameDetails;
}
}
}
return $resultArray;
}
}
?>
# call to the class:
<?php
include("include/TestQueries.php");
$testQueries = new TestQueries();
$nameData = $bvoipQueries->getNameInfo("name", "Fred");
# the array that is returned looks like this:
Array
(
[0] => NameDetails Object
(
[firstName] => Frank
[lastName] => Smith
[age] => 62
)
)
My issue is how to get the values out of the array??? It's the object that is throwing me?
If I try:
if(is_array($nameData)) {
foreach($nameData as $key=>$value) {
if($key == "firstName") {
$firstName = $value;
echo $firstName;
}
}
}
The error I get is:
"Catchable fatal error: Object of class NameDetails could not be converted to string in .....