-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);
Johnny
  • 23
  • 2
  • 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']` – andrewsi Jun 06 '13 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 – Shomz Jun 06 '13 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? – Bad Wolf Jun 06 '13 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? – ASGM Jun 06 '13 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 – Lefsler Jun 06 '13 at 18:59

2 Answers2

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.

Rob W
  • 9,134
  • 1
  • 30
  • 50
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.

bwoebi
  • 23,637
  • 5
  • 58
  • 79
  • 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 Jun 06 '13 at 19:54
  • @Johnny Then return the whole rows: `while ($row = fetch_assoc($query)) { $return_data[] = $row; } return $return_data;` – bwoebi Jun 06 '13 at 19:56
  • 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
    ";}
    – Johnny Jun 06 '13 at 20:01
  • ah, you mean this... then use: `while ($row = fetch_assoc($query)) { $return_data[$row['id']] = $row['name']; }` (or how is your row named?) – bwoebi Jun 06 '13 at 20:04
  • 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 – Johnny Jun 06 '13 at 20:08
  • hey... if the user dont have any players can you make it return false? – Johnny Jun 06 '13 at 21:05
  • Use `return $return_data?:false; ` – bwoebi Jun 07 '13 at 09:09