数组仅返回索引为 0 PHP 处的值
Array only returning value at index of 0 PHP
我正在从我的数据库中获取一个数字数组,并将该数组打印出来,但它只是 return索引 0 处的值,即使其他索引也有值,而且所有他们的 playerName 设置为 $username。请帮忙。谢谢
<?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>";
?>
结果:
13fetch
12fetch
11fetch
10fetch
9fetch
8fetch
7fetch
6fetch
5fetch
4fetch
3fetch
2fetch
1fetch
0fetch1
索引为0的数组return是数字1,但索引为其他数字的数组return值为null,即使[=20中有数字=] 在数据库中,用户名设置为 $username。
你必须对 mysql_fetch_array
使用简单的 while
循环,因为这里 returns 当你不使用循环时,结果行作为数组。
N.B: 同时不需要使用同一个查询两次统计结果。
mysql_fetch_array — Fetch a result row as an associative array, a
numeric array, or both
查看此处示例
http://php.net/manual/en/function.mysql-fetch-array.php
编辑:希望它对你有用,但未经测试。
<?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>";
?>
我正在从我的数据库中获取一个数字数组,并将该数组打印出来,但它只是 return索引 0 处的值,即使其他索引也有值,而且所有他们的 playerName 设置为 $username。请帮忙。谢谢
<?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>";
?>
结果:
13fetch
12fetch
11fetch
10fetch
9fetch
8fetch
7fetch
6fetch
5fetch
4fetch
3fetch
2fetch
1fetch
0fetch1
索引为0的数组return是数字1,但索引为其他数字的数组return值为null,即使[=20中有数字=] 在数据库中,用户名设置为 $username。
你必须对 mysql_fetch_array
使用简单的 while
循环,因为这里 returns 当你不使用循环时,结果行作为数组。
N.B: 同时不需要使用同一个查询两次统计结果。
mysql_fetch_array — Fetch a result row as an associative array, a numeric array, or both
查看此处示例
http://php.net/manual/en/function.mysql-fetch-array.php
编辑:希望它对你有用,但未经测试。
<?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>";
?>