PHP while loop does not return what is the problem

Source: Internet
Author: User
What is the problem with the PHP while loop not being returned?
 Include (' include/config.php ');
Include (' include/dbclass.php ');
Session_Start ();


$db = Db::getinstance ();
$db->check_user_login (); Check if the user is logged in
$db->createcon (); Establish a connection
$user =$_session["User"];
$user = "Libero"; Test temporary use
Execute User Data table to select 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

Class db{
private static $dbhost = "localhost";
private static $dbuser = "root";
private static $password = "";
private static $dbname = "YY";
private static $instance = NULL;


constructor function
Private Function _construct () {}
Instantiation of
public static function getinstance () {
if (self:: $instance = = null) {
Self:: $instance = new db ();
}
Return self:: $instance;
}
Connecting to a database
Public Function Mycon () {
@mysql_connetc (self:: $dbhost, Self:: $dbuser, Self:: $password);

}
Select Database
Public Function Selectdb () {
$mysql _select_db (self:: $dbname);

}

Create a Connection connection database
Public Function Createcon () {

Mysql_connect (self:: $dbhost, Self:: $dbuser, Self:: $password);
mysql_select_db (self:: $dbname);
}


Get $sql result set based on query criteria
Public Function Fetch_array ($sql) {

if ($result = $this->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);


}




To close a database connection

Public function Close () {

return Mysql_close ();

}



}








}














?>



------Solution--------------------
Get $sql result set based on query criteria
Public Function Fetch_array ($sql) {
if ($result = $this->query ($sql)) {// This judgment has been established since the use of while, so it's a dead loop.$rs = Mysql_fetch_array ($result, Mysql_both);
return $rs;
} else {
echo "Data query failed";
}
}
------Solution--------------------
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
------Solution--------------------
 
Public function Fetch_all ($sql) {
if ($result = $this->query ($sql))
{
while ($r = Mysql_fetch_array ($result, Mysql_both)) {
$res [] = $r;
  • 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.