-1

I'm a complete PHP noob (and any other language for that matter), what i wanna know is why will this only return 1? No matter how many players the user has.

Also on SOME pages I wanna display them in a <select> menu how can I do that?

function get_players($aid) {

$query = query("SELECT name FROM player WHERE account_id = $aid");
while ($row = fetch_assoc($query)) {

    return $row['name'];

}


}


echo get_characters(1337);
5
  • 2
    You're returning the first time you run through your loop. You should maybe SELECT count(name) AS total in your query, and then return $row['total'] Commented Jun 6, 2013 at 18:54
  • 1
    return ends the execution of the function, you should put the values in an array or an object and then return it when the loop is done Commented Jun 6, 2013 at 18:54
  • Do you have multiple rows in your database table with the same account_id number? Or better yet can you post your table layout and some sample data? Commented Jun 6, 2013 at 18:55
  • 2
    What will only return 1? You define a function get_players(), but only call get_characters(), which isn't defined in the code you posted. Which function are you talking about? Commented Jun 6, 2013 at 18:55
  • It seems you want to COUNT how many players you have, so use the @andrewsi solution. note that RETURN get out of the function with the value in the line. If you want all players you need to store it on an array and then return that array Commented Jun 6, 2013 at 18:59

2 Answers 2

0

If you return somewhere in a function, it immediately quits the function and doesn't go back to the function. So only the first result will be returned.

Save the data in an array like:

while ($row = fetch_assoc($query)) {
    $return_data[] = $row['name'];
}
// and then return the data:
return $return_data;

Also, if you use PHP 5.5 you can use yield to use the function as a generator.

And don't use mysql_* (deprecated as of PHP 5.5), use mysqli or PDO.


Are you sure you called the right function? There's a function get_players and you call the function get_characters.

Sign up to request clarification or add additional context in comments.

7 Comments

What if I want to get more fields? I want to get row ID to but then I only get one name...and then can i use foreach to get the ID and name?
@Johnny Then return the whole rows: while ($row = fetch_assoc($query)) { $return_data[] = $row; } return $return_data;
gives me ID: 0 | Name: Array ID: 1 | Name: Array ID: 2 | Name: Array when i foreach (get_characters($aid) as $k => $v) { echo "Player ID: $k | Player Name: $v<br>";}
ah, you mean this... then use: while ($row = fetch_assoc($query)) { $return_data[$row['id']] = $row['name']; } (or how is your row named?)
wow! you're the man! been googling like a maniac but cant figure it out. i need some php lessons i know. thanks a bunch man you made my night
|
0

Because you're returning in the middle of a while statement. It will break out of the WHILE() on the first iteration and return the FIRST result.

You should try something like this:

function get_players($aid) {
  $query = query("SELECT name FROM player WHERE account_id = $aid");
  $names = array();
  while ($row = fetch_assoc($query)) {
    $names[] = $row['name'];
  }
}

print_r( get_characters(1337) );

PS: You should not be using mysql_* functions. At the very least, learn MySQLI or PDO.

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.