php while迴圈退不出是什麼有關問題

來源:互聯網
上載者:User
php while迴圈退不出是什麼問題?
include('include/config.php');
include('include/dbclass.php');
session_start();


$db = db::getInstance();
// $db->check_user_login(); //檢查使用者是否登入
$db->createcon(); //建立串連
// $user=$_SESSION["user"];
$user = "libero"; //測試臨時使用
// 執行 user資料表 選取user資料
$sql_user = "select * from user where user = '$user'";
$user_array = $db->fetch_array($sql_user);




// 執行 yytable 資料 擷取 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

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


// 建構函式
private function _construct(){}
// 執行個體化
public static function getInstance(){
if (self::$instance == null){
self::$instance = new db();
}
return self::$instance;
}
//串連資料庫
public function mycon(){
@mysql_connetc(self::$dbhost,self::$dbuser,self::$password);

}
//選擇資料庫
public function selectdb(){
$mysql_select_db(self::$dbname);

}

//建立串連 串連資料庫
public function createcon(){

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


// 根據查詢條件擷取 $sql 結果集
public function fetch_array($sql){

if($result = $this->query($sql))
{
$rs = mysql_fetch_array($result, MYSQL_BOTH);
return $rs;

}
else { echo "資料查詢失敗"; }

}

// 資料庫查詢執行語句
public function query($sql){
mysql_query("set names utf8");
return mysql_query($sql);
}

// loop 更具結果集 擷取數組

public function loop_query($result){
return mysql_fetch_array($result);


}




//關閉資料庫連接

public function close(){

return mysql_close();

}



}








}














?>



------解決方案--------------------
// 根據查詢條件擷取 $sql 結果集
public function fetch_array($sql){
if($result = $this->query($sql)) { //用while之後 這個判斷一直都是成立的 所以就成死迴圈了 $rs = mysql_fetch_array($result, MYSQL_BOTH);
return $rs;
} else {
echo "資料查詢失敗";
}
}
------解決方案--------------------
while($yytable_array = $db->fetch_array($sql_yytable)){
總是在執行查詢 $db->fetch_array($sql_yytable) 並總是能返回第一條結果

你的這個 fetch_array 方法只能用於查詢一條記錄的場合
你應該再寫一個 fetch_all 方法,查詢並返回全部結果
------解決方案--------------------

public function fetch_all($sql){
if($result = $this->query($sql))
{
while($r = mysql_fetch_array($result, MYSQL_BOTH)) {
$res[] = $r;
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.