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"
". $yytable _array[0]." | "; Echo"
". $yytable _array[1]." | "; Echo"
". $yytable _array[2]." | "; Echo"
". $yytable _array[3]." | "; Echo"
". $yytable _array[4]." | "; Echo"
". $yytable _array[5]." | "; Echo"
";} echo "
";
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;