-2
以dB如果ISSET不工作?
row1 (type column=apple), (stage column=1)
row2 (type column=orange), (stage column=NULL)
在PHP
$query = mysql_query("SELECT * from fruits");
$row = mysql_fetch_array($query);
$num = mysql_num_rows($query);
$i=0;
$storeMyData = array();
while($row = mysql_fetch_array($query))
{
if(isset($row['type'])){
$type = "-" . $row['type'];
} else {
$type = NULL;
}
if(isset($row['stage'])){
$stage = "STAGE " . $row['stage'];
} else {
$stage = NULL;
}
$fruit = implode (", ", array_filter(array($type, $stage)));
// This will show 2 rows of data
$storeMyData[] = $fruit;
// store current data in array
$i++;
}
/* this will echo your storedData of loop */
foreach($storeMyData as $prevData)
/* or join the data using string concatenation */
$allFinalData2 = "";
/* this will echo your storedData of loop */
foreach($storeMyData as $prevData)
{
$allFinalData2 = $allFinalData2.$prevData ; // keep on concatenating
}
echo $allFinalData2;
最终输出显示为 “苹果,第1阶段”, “橙色舞台”。
如果在DB中 row2(orange)的阶段为NULL,我该如何显示它没有单词阶段的“Apple,Stage 1”“Orange”。
我已经将我的$行设置为mysql_fetch_array($ query);已经,但仍然不工作:( –
传递给mysql_fetch_array的变量必须与代码的其余部分匹配,在顶部有mysql_num_rows($ query),因此您应该将$ query变量传递给mysql_fetch_array。 发布更新代码清单所以我们可以仔细检查 – JConstantine
查询的第二行只显示没有阶段,这是好的。但是我需要显示两行 –