Skip to content
Advertisement

PHP populate array inside a function

i have a small problem with my php code.

i’ve created a class for Clients, to return me a list with all clients. The problem is when i call the function from outsite, it dont show me the clietsList.

<?php 

$returnData = array();

class Clients
{
     public function readAll(){
        $query = "SELECT * FROM clients";
        $result = $this->con->query($query);
        if ($result->num_rows > 0){
            while ($row = $result->fetch_assoc()){
                $returnData['clientsList'][] = $row;
            }
            return $returnData;
        }
        else{
            echo "No found records";
        }
    }
}


if ($auth->isAuth()){
    $returnData['message'] = "you are loggedin, so is ok";
    $clientsObj = new Clients();
    $clientsObj->readAll();
}

echo json_encode($returnData);
?>

and the result is like that, but without clietslist

{
  "message": "you are loggedin, so is ok"
}

Where im doing wrong? Thanks for your answer. i want to learn php, im begginer. thanks in advance.

Advertisement

Answer

You need to get return value of function in a variable:

$returnData['message'] = "you are loggedin, so is ok";
$clientsObj = new Clients();
$returnData['clients'] =    $clientsObj->readAll();
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement