What is the problem that the phpwhile loop does not return?

Source: Internet
Author: User
What is the problem that the phpwhile loop does not return?
 Check_user_login (); // check whether the user has logged on to $ db-> createcon (); // establish a connection // $ user = $ _ SESSION ["user"]; $ user = "libero"; // Test temporary use // run the user data table to select user Data $ SQL _user = "select * from user where user = '$ user '"; $ user_array = $ db-> fetch_array ($ SQL _user ); // execute yytable data to obtain all data in yytable $ SQL _yytable = "select * from yytable where uid = '$ user_array [0]'"; echo"
 
 
  
  
"; While ($ yytable_array = $ db-> fetch_array ($ SQL _yytable) {echo"
  
  
     "; Echo" 
    "; Echo" 
    "; Echo" 
    "; Echo" 
    "; Echo" 
    "; Echo" 
    "; Echo" 
   ";} Echo" 
  
". $ Yytable_array [0]."". $ Yytable_array [1]."". $ Yytable_array [2]."". $ Yytable_array [3]."". $ Yytable_array [4]."". $ Yytable_array [5]."
"; // Dbclass Query ($ SQL) {$ rs = mysql_fetch_array ($ result, MYSQL_BOTH); return $ rs ;}else {echo "data query failed ";}} // database query execution statement public function query ($ SQL) {mysql_query ("set names utf8"); return mysql_query ($ SQL );} // loop more result set get array public function loop_query ($ result) {return mysql_fetch_array ($ result);} // close database connection public function close () {return mysql_close () ;}}?>


Reply to discussion (solution)

while($yytable_array = $db->fetch_array($sql_yytable))

When the SQL statement $ SQL _yytable is incorrect, "Data Query failed" is returned"

The while statement is a pay-as-you-go expression, so it is always true.

Print the SQL statement $ SQL _yytable = "select * from yytable where uid = '$ user_array [0]'"; then run the following command in the database to check the result:

// Obtain the $ SQL result set based on the query conditions
Public function fetch_array ($ SQL ){
If ($ result = $ this-> query ($ SQL )) {// After The while clause is used, the result is always an endless loop. $ rs = mysql_fetch_array ($ result, MYSQL_BOTH );
Return $ rs;
} Else {
Echo "data query failed ";
}
}

While ($ yytable_array = $ db-> fetch_array ($ SQL _yytable )){
Always query $ db-> fetch_array ($ SQL _yytable) and always return the first result.

Your fetch_array method can only be used to query a record.
You should write another fetch_all method to query and return all results.

While ($ yytable_array = $ db-> fetch_array ($ SQL _yytable )){
Always query $ db-> fetch_array ($ SQL _yytable) and always return the first result.

Your fetch_array method can only be used to query a record.
You should write another fetch_all method to query and return all results. what you said about me is that only the first statement is cyclically output. if you want to loop all other results, please express

// Obtain the $ SQL result set based on the query conditions
Public function fetch_array ($ SQL ){
If ($ result = $ this-> query ($ SQL )) {// After The while clause is used, the result is always an endless loop. $ rs = mysql_fetch_array ($ result, MYSQL_BOTH );
Return $ rs;
} Else {
Echo "data query failed ";
}
} I know where the problem is. the key is how to solve it. I want to output all results that meet the query conditions.

Public function fetch_all ($ SQL) {if ($ result = $ this-> query ($ SQL) {while ($ r = mysql_fetch_array ($ result, MYSQL_BOTH )) {$ res [] = $ r;} return $ res;} else {echo "data query failed ";}}

Of course, the returned result is an array. you need to follow the array for subsequent work.

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.