About the foreach output problem

Source: Internet
Author: User
$sql 1= "SELECT * from Y_cate ORDER by ASC";
$result 1=mysql_fetch_array (mysql_query ($sql 1));
Var_dump ($result 1);
foreach ($result 1 as $ke = = $a) {
echo $a. "
";
}
?>
I want to use foreach to loop through the output of all the data in the Cate table, why just output one piece of data? Ask for answers


Reply to discussion (solution)

$sql 1= "SELECT * from Y_cate ORDER by ASC"; $result 1 = mysql_query ($sql 1), while ($row = mysql_fetch_array ($result 1)) {  F Oreach ($row as $ke = + $a) {    echo $a. "
"; }}

$sql 1= "SELECT * from Y_cate ORDER by ASC";
$result 1=mysql_fetch_array (mysql_query ($sql 1));
Var_dump ($result 1);
foreach ($result 1 as $ke = = $a) {
echo $a. "
";
}
?>
I want to use foreach to loop through the output of all the data in the Cate table, why just output one piece of data? Ask for answer 1. Your SQL statement will not get an error? ORDER by ASC, no sort field;
2. No SQL statement executed. mysql_query ($sql 1).


$sql 1= "SELECT * from Y_cate ORDER by ASC";
$result 1=mysql_fetch_array (mysql_query ($sql 1));
Var_dump ($result 1);
foreach ($result 1 as $ke = = $a) {
echo $a. "
";
}
?>
I want to use foreach to loop through the output of all the data in the Cate table, why just output one piece of data? Ask for answer 1. Your SQL statement will not get an error? ORDER by ASC, no sort field;
2. No SQL statement executed. mysql_query ($sql 1). Hand wrong hand?

SELECT * FROM Y_cate ORDER by ASC problem

The order by does not specify a field and will error: #1064-you has an error in your SQL syntax; Check the manual, corresponds to your MySQL server version for the right syntax to use near ".....

$sql 1= "SELECT * from Y_cate ORDER by ASC"; $result 1 = mysql_query ($sql 1), while ($row = mysql_fetch_array ($result 1)) {  F Oreach ($row as $ke = + $a) {    echo $a. "
"; }}

Do you have to use while?
If you use while,
[Code] $sql 1= "SELECT * from Y_cate ORDER by px ASC";
$US =mysql_query ($sql 1);
while ($result 1=mysql_fetch_array ($US)) {
echo $result 1[' name '];

}[/code]
This is not a more concise way?? And it looks like you're going to have to repeat the output when you output it, here's the output data, repeat
43
43
Website Home
Website Home
50b5b9555f97e.png
50b5b9555f97e.png
0
0
0
0

SELECT * FROM Y_cate ORDER by ASC problem

The order by does not specify a field and will error: #1064-you has an error in your SQL syntax; Check the manual, corresponds to your MySQL server version for the right syntax to use near ".....
Pro, hand-knock code, hand missed out. May I ask the question how to solve


$sql 1= "SELECT * from Y_cate ORDER by ASC"; $result 1 = mysql_query ($sql 1), while ($row = mysql_fetch_array ($result 1)) {  F Oreach ($row as $ke = + $a) {    echo $a. "
"; }}

Do you have to use while?
If you use while,
[Code] $sql 1= "SELECT * from Y_cate ORDER by px ASC";
$US =mysql_query ($sql 1);
while ($result 1=mysql_fetch_array ($US)) {
echo $result 1[' name '];

}[/code]
This is not a more concise way?? And it looks like you're going to have to repeat the output when you output it, here's the output data, repeat
43
43
Website Home
Website Home
50b5b9555f97e.png
50b5b9555f97e.png
0
0
0
0

Repeat is because you use the Mysql_fetch_array replaced MYSQL_FETCH_ASSOC after not repeat, specific reasons please Baidu under these two functions, a look on the understanding.

Version of the big give the method is correct, you want to output each record of each item, this is two loops, while take each one, foreach take each item. Once you've changed it, you're circulating it. Of course, you can't fulfill your needs.

while ($row = mysql_fetch_array ($result 1,mysql_assoc)) {
foreach ($row as $ke = = $a) {
echo $a. "
";
}
}

or just 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.