关于foreach输出问题
$result1=mysql_fetch_array(mysql_query($sql1));
var_dump($result1);
foreach($result1 as $ke=>$a){
echo $a."
";
}
?>
我想用foreach 来循环输出cate表的所有数据,为什么只输出一条数据的?求解答
回复讨论(解决方案)
$sql1="select * from y_cate order by asc";$result1 = mysql_query($sql1);while($row = mysql_fetch_array($result1)) { foreach($row as $ke=>$a){ echo $a."
"; }}
$sql1="select * from y_cate order by asc";
$result1=mysql_fetch_array(mysql_query($sql1));
var_dump($result1);
foreach($result1 as $ke=>$a){
echo $a."
";
}
?>
我想用foreach 来循环输出cate表的所有数据,为什么只输出一条数据的?求解答 1. 你的sql语句不会报错?order by asc,没有排序字段;
2. 没有执行 sql语句。mysql_query($sql1).
$sql1="select * from y_cate order by asc";
$result1=mysql_fetch_array(mysql_query($sql1));
var_dump($result1);
foreach($result1 as $ke=>$a){
echo $a."
";
}
?>
我想用foreach 来循环输出cate表的所有数据,为什么只输出一条数据的?求解答 1. 你的sql语句不会报错?order by asc,没有排序字段;
2. 没有执行 sql语句。mysql_query($sql1). 手误手误 ?
select * from y_cate order by asc的问题
order by没有指定字段,会报错:#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near "..........
$sql1="select * from y_cate order by asc";$result1 = mysql_query($sql1);while($row = mysql_fetch_array($result1)) { foreach($row as $ke=>$a){ echo $a."
"; }}
必须要用while么?
如果用while的话
[code] $sql1="select * from y_cate order by px asc";
$us=mysql_query($sql1);
while($result1=mysql_fetch_array($us)){
echo $result1['name'];
}[/code]
这样子不是更简洁??而且好像你那个方法输出的时候会重复输出,下面是输出的数据,重复了一次
43
43
网站首页
网站首页
50b5b9555f97e.png
50b5b9555f97e.png
0
0
0
0
select * from y_cate order by asc的问题
order by没有指定字段,会报错:#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near "..........
亲,手敲的代码,手误漏掉了。请问我问的问题怎么解决
$sql1="select * from y_cate order by asc";$result1 = mysql_query($sql1);while($row = mysql_fetch_array($result1)) { foreach($row as $ke=>$a){ echo $a."
"; }}
必须要用while么?
如果用while的话
[code] $sql1="select * from y_cate order by px asc";
$us=mysql_query($sql1);
while($result1=mysql_fetch_array($us)){
echo $result1['name'];
}[/code]
这样子不是更简洁??而且好像你那个方法输出的时候会重复输出,下面是输出的数据,重复了一次
43
43
网站首页
网站首页
50b5b9555f97e.png
50b5b9555f97e.png
0
0
0
0
重复是因为你用了 mysql_fetch_array 换成 mysql_fetch_assoc后就不重复了,具体原因请百度下这两个函数,一看就明白.
版大给出的方法是正确的,你要输出每一条记录的每一项,这就是两个循环,while取每一条,foreach取每一项. 你改完的就循环了一次 当然不能实现你的需求啦
while($row = mysql_fetch_array($result1,MYSQL_ASSOC)) {
foreach($row as $ke=>$a){
echo $a."
";
}
}
或者就用mysql_fetch_assoc/mysql_fetch_row