What is the problem with the PHP while loop not being returned?

Source: Internet
Author: User
 Check_user_login ();            Check whether the user is logged in $db->createcon ();           Establish a connection//$user =$_session["user"]; $user = "Libero"; The test temporarily uses//executes the User data table to select the user data $sql_user = "SELECT * from user where user = ' $user '"; $user _array = $db->fetch_array ($ Sql_user);//Perform yytable data acquisition yytable all data $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 gets 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))

First consider $sql_yytable this SQL statement is wrong at this time will return "data query failed"

The while statement is a pay-value expression and is always true

Print out the SQL statement $sql_yytable = "SELECT * from yytable where uid = ' $user _array[0] '"; Then go to the database and perform the following to see the results

Get $sql result set based on query criteria
Public Function Fetch_array ($sql) {
if ($result = $this->query ($sql)) {//use while after this judgment has been established so it becomes a dead loop $rs = Mysql_fetch_array ($result, Mysql_both);
return $rs;
} else {
echo "Data query failed";
}
}

while ($yytable _array = $db->fetch_array ($sql _yytable)) {
Always executes the query $db->fetch_array ($sql _yytable) and always returns the first result

Your Fetch_array method can only be used to query a record of the occasion
You should write another Fetch_all method, query and return all results

while ($yytable _array = $db->fetch_array ($sql _yytable)) {
Always executes the query $db->fetch_array ($sql _yytable) and always returns the first result

Your Fetch_array method can only be used to query a record of the occasion
You should write another Fetch_all method, query and return all results great God, you're right. I have been only looping through the first statement, if you want to loop out all the other results, specifically, please express

Get $sql result set based on query criteria
Public Function Fetch_array ($sql) {
if ($result = $this->query ($sql)) {//use while after this judgment has been established so it becomes a dead loop $rs = Mysql_fetch_array ($result, Mysql_both);
return $rs;
} else {
echo "Data query failed";
}
} know where the problem is, the key is how to solve ah I want to output all the results that meet the query criteria

  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 return is an array, you have to follow the array to do the 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.