About foreach output

Source: Internet
Author: User
About foreach output $ Sql1 = "select * from y_cate order by asc ";
$ Result1 = mysql_fetch_array (mysql_query ($ sql1 ));
Var_dump ($ result1 );
Foreach ($ result1 as $ ke => $ ){
Echo $ ."
";
}
?>
I want to use foreach to output all the data in the cate table cyclically. Why does it output only one piece of data? Answer


Reply to discussion (solution)

$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 => $ ){
Echo $ ."
";
}
?>
I want to use foreach to output all the data in the cate table cyclically. Why does it output only one piece of data? 1. will your SQL statement return no error? Order by asc, no sorting field;
2. no SQL statement is executed. 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 => $ ){
Echo $ ."
";
}
?>
I want to use foreach to output all the data in the cate table cyclically. Why does it output only one piece of data? 1. will your SQL statement return no error? Order by asc, no sorting field;
2. no SQL statement is executed. Mysql_query ($ sql1). What is the incorrect hand?

Select * from y_cate order by asc

If no field is specified for order by, the following error occurs: #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."
"; }}

Must I use while?
If the while clause is used
[Code] $ sql1 = "select * from y_cate order by px asc ";
$ Us = mysql_query ($ sql1 );
While ($ result1 = mysql_fetch_array ($ us )){
Echo $ result1 ['name'];

} [/Code]
Isn't it more concise ?? And it seems that you will repeat the output in that method. below is the output data, repeat once.
43
43
Homepage
Homepage
50b5b9555f97e.png
50b5b9555f97e.png
0
0
0
0

Select * from y_cate order by asc

If no field is specified for order by, the following error occurs: #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 "..........
Dear user, the code of manual knock is Missing by mistake. How can I solve the problem?


$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."
"; }}

Must I use while?
If the while clause is used
[Code] $ sql1 = "select * from y_cate order by px asc ";
$ Us = mysql_query ($ sql1 );
While ($ result1 = mysql_fetch_array ($ us )){
Echo $ result1 ['name'];

} [/Code]
Isn't it more concise ?? And it seems that you will repeat the output in that method. below is the output data, repeat once.
43
43
Homepage
Homepage
50b5b9555f97e.png
50b5b9555f97e.png
0
0
0
0

Repeat is because you have replaced mysql_fetch_array with mysql_fetch_assoc without repeating it. for specific reasons, please refer to Baidu's two functions.

The method provided by maxcompute is correct. you need to output each item of each record. this is two loops. while takes each entry, foreach takes each entry. once you finish the change, you will loop through it. of course, you cannot meet your needs.

While ($ row = mysql_fetch_array ($ result1, MYSQL_ASSOC )){
Foreach ($ row as $ ke => $ ){
Echo $ ."
";
}
}

Or use mysql_fetch_assoc/mysql_fetch_row.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.