1

I have a function in a class called database, the function as below

function getTaskData(){ 
    $q = "SELECT * FROM ".TBL_CLIENT ;
    $result = mysqli_query( $this->connection,$q);
    if(!$result || (mysqli_num_rows($result) < 1)){
        return NULL;
    }
    /* Return result array */
    ;
    while ($dbarray = mysqli_fetch_array($result)) {
        return $dbarray;    
    }
}

Please note that $dbarray is from while loop, however, i want to call the function to loop out specific data from the table in a while loop

please see below, i am calling the function from another page

$taskData= $database->getTaskData();
$taskStatus = $taskData['status'];
echo $taskStatus

I want $taskStatus to be in a loop.

2
  • You either collect all rows to one array and return it from your function, or return $result from your function and iterate it where you need. Commented Nov 6, 2018 at 15:45
  • @u_mulder please explain better or give example Commented Nov 6, 2018 at 15:48

1 Answer 1

1

Replace your loop in a function with:

$results = [];
while ($dbarray = mysqli_fetch_array($result)) {
    $results[] = $dbarray;    
}

return $results;

Somewhere else:

$taskData = $database->getTaskData();
foreach ($taskData as $item) {
    echo $item['status'];
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.