php - function to get data from database

39

I would like to build a function that get data from database and return it, then i would like to call this function into a while.

i do this function but give me some problems when i call it from a while:

public function getAllUsers() {

    // if database connection opened
    if ($this->databaseConnection()) {

        // database query, getting all the info of the selected user
        $query = $this->db_connection->prepare('SELECT * FROM users');
        $query->execute();

        // get result row (as an object)
        return $query->fetchObject();

    } else {
        return false;
    } 
}

and these is the way i would like to call the function:

while($row = $users->getAllUsers()) {
    echo "User name: ".$row->user_name."<br>User ID: ".$row->user_id;
}

Can anyone help please?

293

Answer

Solution:

There are two problem in your code:

  1. ThegetAllUsers() method return only the first record of query, not all;
  2. even if you fix this, yourwhile loop - as noted in another answer - never ends, because retrieve infinitely a result.

To fix it, you have to change your code in this way:

public function getAllUsers() {
    (...)
    // get all rows (as an object)
    return $query->fetchAll( PDO::FETCH_OBJ );
    (...)
}

and then perform aforeach loop instead of thewhile one:

foreach( $users->getAllUsers() as $row ) {
    (...)
}

Alternative: you can return the$query object and fetch-it out of the method:

public function getAllUsers() {
    (...)
    // return the query:
    return $query;
    (...)
}

$rows = $users->getAllUsers();
while( $row = $rows->fetchObject() ) {
    echo "User name: ".$row->user_name."<br>User ID: ".$row->user_id;
}

The first solution is more congruent and pretty, the second can more performative on very large number of rows returned by the query

346

Answer

Solution:

What you're trying to do is getting all users, and then loop over all your users and echo some information about them. Your functiongetAllUsers() retrieves all users with your SQL query. However, your while loop executes your function many times, each time getting the entire user list.

Instead, execute your function once, get the result from it, and then loop over the result.

$userlist = $users->getAllUsers();

while($row = $userlist->getRow()) { //Im not sure what the function is to get a row in the framework you're using.
    echo "User name: ".$row->user_name."<br>User ID: ".$row->user_id;
}

Look up how a while loop works

The predicate of a while loop ($row = $users->getAllUsers()) is run each iteration, so calling a function in the predicate also executes that function each iteration.

People are also looking for solutions to the problem: php - Query redirect and clean up

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.