我有下面的代碼設置和mysql_num_rows回聲2,因爲它應該是。 我想以某種方式訪問已被選中的第二行,但無法弄清楚如何。 echo $ lTitl總是以隨機順序從第一行返回標題,但我還想在此之後訪問行。我該怎麼做呢?如何在選擇它們後訪問所有行的列值?
$sql = "SELECT id, seller, title, kondition, price, shippingcost FROM items WHERE active='1' ORDER BY rand() LIMIT 2";
$item_query = mysql_query($sql, $connect);
foreach ($row = mysql_fetch_assoc($item_query) as $test) {
$id = $row["id"];
$seller = $row["seller"];
$lTitl = $row["title"];
$lCond = $row["kondition"];
//$lPrimIma = $row["primaryimage"];
$lPric = $row["price"];
$lShip = $row["shippingcost"];
}
echo $lTitl;
mysql_fetch_assoc()只返回一個行,所以你的foreach(mysql_fetch_assoc()爲$測試)只會循環一次。你想mysql_fetch_all – Rottingham
看到我的答案@willi http://stackoverflow.com/a/21056095/1607528 – 2014-01-10 23:07:41