0

I'm fetching an array of numbers from my database, and printing the array out, but it's only returning the value at index of 0, even though the other indexes has values too, and all their playerName is set to $username. Please help. Thanks.

<?php

session_start(); 
$username = $_SESSION['username'];

$fetch = mysql_query("SELECT amount FROM playerDeck WHERE playerName = '$username'");
$count = mysql_num_rows($fetch);
$fetch = mysql_fetch_array(mysql_query("SELECT amount FROM playerDeck WHERE playerName = '$username'"));

while($count > 0){
$count--;

echo "$count";
echo "fetch$fetch[$count]</br>";

?>

Results:

13fetch
12fetch
11fetch
10fetch
9fetch
8fetch
7fetch
6fetch
5fetch
4fetch
3fetch
2fetch
1fetch
0fetch1

The array with index of 0 returns the number 1, but the array with the index of other numbers return with the value null, even though there are numbers in the table inside the database with the username set to $username.

Benoit Esnard
  • 2,017
  • 2
  • 24
  • 32
iscattered
  • 103
  • 1
  • 10
  • you should not be using mysqli_* functions because they are deprecated. See http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – BRBT May 15 '15 at 16:08

1 Answers1

0

You have to use simple while loop for mysql_fetch_array because here it returns a result row as array when you use it without loop.

N.B: At the same time no need to use same query two time to count result.

mysql_fetch_array — Fetch a result row as an associative array, a numeric array, or both

See Here Examples

http://php.net/manual/en/function.mysql-fetch-array.php

EDIT: Hope it will work for you but not tested.

<?php

session_start(); 
$username = $_SESSION['username'];

$fetch = mysql_query("SELECT amount FROM playerDeck WHERE playerName = '$username'");
$count = mysql_num_rows($fetch);

while($row=mysql_fetch_array($fetch,MYSQL_NUM)){
  $count--;
  echo "$count";
  echo "fetch $row[0]</br>";
?>
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103