好吧,我一直在网站上工作,但每当我对来自数据库的一些数据使用foreach循环时,我一直都会出现奇怪的显示。 我明白了:
2
Posted By: 2
22
Posted By: 2
2H
Posted By: H
HH
Posted By: H
HT
Posted By: T
TT
Posted By: T
TA
Posted By: A
AA
Posted By: A
A
当我使用这个循环时:
$res = mysql_query("SELECT * FROM `fcat`");
$resa = mysql_fetch_array($res);
if(!empty($resa)){
foreach($resa as $j){
echo $j['title']."<br />";
echo "Posted By: ".$j['poster']."<br />";
echo $j['info'];
}
}else{
echo "Nothing.";
}
在此数据库中:
id is an int(11)= 2
title is a varchar(65)= Hi
info is a text= Test
creator is a text= Admin
谢谢!
答案 0 :(得分:1)
我认为这可能会对您有所帮助:
$res = mysql_query("SELECT * FROM `fcat`");
if(mysql_num_rows($res)>0){
while($j = mysql_fetch_array($res)){
echo $j['title']."<br />";
echo "Posted By: ".$j['poster']."<br />";
echo $j['info'];
}
}else{
echo "Nothing.";
}
答案 1 :(得分:0)
mysql_fetch_array返回一个包含数字和关键字的数组
数组mysql_fetch_array(resource $ result [,int $ result_type = MYSQL_BOTH])
所以,将其改为
mysql_fetch_array($res, MYSQL_ASSOC);
答案 2 :(得分:0)
试试这个:
$res = mysql_query("SELECT * FROM `fcat`");
while($j = mysql_fetch_array($res)){
echo $j['title']."<br />";
echo "Posted By: ".$j['poster']."<br />";
echo $j['info'];
}