我已经用问题注释了我的代码,关于解决这个问题有什么建议吗?已经被困在上面几个小时了。
<?php
include("header.php");
$inv = mysql_query("SELECT `item_id` FROM `user_items` WHERE `user_id`='".$_SESSION['uid']."'") or die(mysql_error());
$linv = mysql_fetch_array($inv);
//Need to display all item_id's from the user_items table where the user_id field = $_SESSION['uid'] (or 1 for testing purposes.)
//For some reason, every foreach and while loop method i have tried is giing me false data (1,1). The right data should be: 1, 4.
//The database table contains 3 columns: slot_id, user_id, item_id.
//Those columns contain the following data: 1 1 1
// 2 2 4
// 3 1 4
//surely this should return $linv as an array containing the values 1 and 4? What's going on?
}
include("footer.php");
?>
change
$linv = mysql_fetch_array($inv);
while(list($key, $value) = each($linv))
{
echo($value);
}
while($linv = mysql_fetch_array($inv))
{
echo $linv['item_id'] . ' ';
}
编辑:问题原因
-第一个问题是$linv = mysql_fetch_array
只返回下一行,并将其作为数组存储在$linv
中,在一开始它从# 0行开始
第一个问题是你缺少一个循环来检索所有行
-现在你可能会问你自己"如果我只得到一行,那么为什么输出是1 1",这是因为mysql_fetch_array
接受第二个参数,这损害了获取模式(MYSQL_ASSOC为关联数组,只能访问查询中的列名,MYSQL_NUMERIC为数字数组,只能访问数字0,1,2…以相同的顺序列在选择子句中指定),当你不传递该参数时,MYSQL_BOTH将默认使用,你将得到像["0" => "1","item_id" => "1"]这样的第一行
你所做的只是循环遍历第一行中的所有元素