我正在从我的数据库中获取一个数字数组,并将数组打印出来,但是它只是在索引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的数组返回数字1,但是具有其他数字索引的数组返回值为null,即使数据库中有将用户名设置为$username的数字。
发布于 2015-05-15 15:57:04
您必须对while使用简单的mysql_fetch_array循环,因为在这里,当您使用不带循环的结果行时,它作为数组返回结果行。
N.B:在同一时间不需要使用相同的查询来计数结果。
mysql_fetch_array -将结果行提取为关联数组、数字数组或两者兼有。
见这里的例子
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>";
?>https://stackoverflow.com/questions/30263763
复制相似问题