0

I have faced a problem in returning a multiple values from the database in an array because every time i run this code it returns only the first row from the database but not all the rows and here is my code:

In classes.php:

class user{
    public function showwinners(){
        $query="SELECT points,memberid,uname FROM user ";
        $all_answers=array();
        if($query_run=mysql_query($query)){
            if(mysql_num_rows($query_run)==NULL){
                return 0;
            }
            while($query_row=mysql_fetch_assoc($query_run)){
                $one=$query_row['points'];
                $two=$query_row['memberid'];
                $three=$query_row['uname'];
                if($one>=1){
                    $first=$one;
                    $second=$three;
                    array_push($all_answers,['name'=>$second,'points'=>$first]);
                    return $all_answers;
                }
            }
        }
    }
}

and in showwinners.php:

$userobj=new user();
$arr=array();
$arr=$userobj->showwinners();
foreach ($arr as $key => $a) {
    if(isset($a['name']))
        echo $a['name'];
        if(isset($a['points']))
            echo $a['points'];
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
msab
  • 27
  • 1
  • 6
  • 3
    Your return statement is inside your while loop. Move it 1 line lower (under the `}`) and it should be fine. – Dirk Scholten Aug 08 '18 at 08:13
  • And also learn to __format__ your code, it will help you in the future. – u_mulder Aug 08 '18 at 08:14
  • thanks for your advice i did what you told me but there is no change in the result – msab Aug 08 '18 at 08:17
  • 1
    [Why shouldn't I use mysql_* functions in PHP?](https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – AymDev Aug 08 '18 at 08:17
  • you array will only populate if this condition is met `if ($one >= 1)`. If you are only getting one row, even after removing the `return` outside of the loop, it means that `$one` is greater or equal to `1` only once, hence the one row result. – CodeGodie Aug 08 '18 at 08:23
  • Some sensible code indentation would be a good idea. It helps us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](http://www.php-fig.org/psr/psr-2/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Aug 08 '18 at 08:31

2 Answers2

1

Give this a try

class user{
  /**
   * @return array
   */
  public function showwinners(){
    $query = "SELECT points, memberid, uname FROM user";
    $all_answers = array();
    if( $query_run = mysql_query( $query ) ){
      if( mysql_num_rows( $query_run ) == NULL ){
         return 0;
      }
      while( $query_row = mysql_fetch_assoc( $query_run ) ){
        $one = $query_row['points'];
        $two = $query_row['memberid'];
        $three = $query_row['uname'];
        if( $one >=1 ){
          $first = $one;
          $second = $three;
          array_push( $all_answers, ['name'=>$second,'points'=>$first] );
        }
      }
    }

    return $all_answers;
  }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
virtualLast
  • 573
  • 1
  • 5
  • 19
  • yes it works now :D thank you very very much – msab Aug 08 '18 at 08:23
  • 3
    @virtualLast _"give this a try"_ is not a good answer. Even though your code snippet seemed to satisfy the OP, you still need to provide details explaining why this would work, where did they go wrong, and what you did to fix it. Giving someone a snippet of code helps them alone, not the community. – CodeGodie Aug 08 '18 at 08:28
  • @CodeGodie I'll keep that in mind for next time ;) – virtualLast Aug 08 '18 at 08:32
  • 3
    Or you could fix this answer now ! – RiggsFolly Aug 08 '18 at 08:32
-2

Move the return statement under the second "}" In this way it will be outside the while loop.

Kronos24
  • 43
  • 7