0

I want to select row by row and set to array in MYSQL

I have this code:

  public function getWinnerBonus($email){
    $result = mysql_query("SELECT * FROM winers WHERE `email` = '$email'") or die(mysql_error());
    // return user details
    if ($result){
        $respons               = mysql_fetch_array($result);
        $response["error"] = FALSE;
        $response["WinnerInfo"]["price"] = $respons["price"];
        $response["WinnerInfo"]["level"] = $respons["level"];
        $response["WinnerInfo"]["win_date"] = $respons["win_date"];
        $response["WinnerInfo"]["payed"] = $respons["payed"];
        $response["WinnerInfo"]["pay_date"] = $respons["pay_date"];

    }else{
        $response["error"] = TRUE;
    }
    return $response;
}

but this code return just first founded row

How to select row by row and set to array in MYSQL?

shirin
  • 129
  • 1
  • 9
  • Sidenote: `GROUP BY` comes after a `WHERE` clause in a `SELECT`. https://dev.mysql.com/doc/refman/5.0/en/select.html - Your `mysql_error()` should have thrown you a syntax error, but you didn't share that with us. – Funk Forty Niner Oct 30 '15 at 12:02
  • *"but this code return just first founded row"* - I sincerely doubt that. – Funk Forty Niner Oct 30 '15 at 12:07

2 Answers2

1

You need to loop over the result set not just one time.

$respons = array();
$i = 0;
while ($row = mysql_fetch_assoc($result)) {
  $respons[$i]["error"] = FALSE;
  $respons[$i]["WinnerInfo"]["price"] = $row["price"];
  $respons[$i]["WinnerInfo"]["level"] = $row["level"];
  $respons[$i]["WinnerInfo"]["win_date"] = $row["win_date"];
  $respons[$i]["WinnerInfo"]["payed"] = $row["payed"];
  $respons[$i]["WinnerInfo"]["pay_date"] = $row["pay_date"];
  ++$i;
}

Note: Never use mysql_ functions, they are deprecated. Use mysqli_ or PDO instead, along with a prepared statement.

References:

Also make sure that you have successfully connected to your database using the same API as your query.

Different MySQL APIs do not intermix.

Consult the following:

Community
  • 1
  • 1
Pupil
  • 23,834
  • 6
  • 44
  • 66
0

Yes, because you have to loop over the result set. Here is how you could do it:

$resultArray = array();
while($response = mysql_fetch_array($result, MYSQL_NUM) {
    $resultArray[] = $response; //This adds one result set to the array
}

I hope I could help you.

Basti909851
  • 127
  • 8