簡體   English   中英

php類僅返回結果的第一行

[英]php class only returns 1st line of results

所以基本上我有一個數據庫表:

id,user_id,職業,年份,最高位置

我創建了一個類:

class WorkHistory {
    public $id;
    public $user_id;
    public $ocupation;
    public $years;
    public $highest_position;

    //construct work history based on user id
    public function __construct($id=null){
        $this->id = $id;
        WorkHistoryWrapper::getHistory($this);
    }

    //update user history
//    public function update(){
//        UserWrapper::updateHistory($this);
//    }
}


class WorkHistoryWrapper extends WorkHistory {
    //get user work history
    static public function getHistory($WorkHistory){
        global $mysqli;

        //get user info according to user id
        $WorkHistoryQuery = $mysqli->prepare("SELECT user_id, ocupation, years, highest_position
        FROM work_history
      WHERE user_id = ? LIMIT 200");

        $WorkHistoryQuery->bind_param('s', $WorkHistory->id);  // Bind "$id" to parameter.
        $WorkHistoryQuery->execute();    // Execute the prepared query.
        $WorkHistoryQuery->store_result();

        // get variables from result.
        $WorkHistoryQuery->bind_result($WorkHistory->user_id, $WorkHistory->ocupation, $WorkHistory->years, $WorkHistory->highest_position);
        $WorkHistoryQuery->fetch();
        return $WorkHistory;
    }
}

然后我通過執行以下操作來拉班:

$wh = new WorkHistory($logged_user);

當我做:

print_r($wh);

它返回:

WorkHistory對象([id] => 3 [user_id] => 3 [職業] =>忍者[年] => 100 [最高位置] =>黑帶)

但是,這只是第一個結果,我如何獲得其他結果?

歡呼,丹

$WorkHistoryQuery->fetch(); 除非您使用while循環,否則只會返回一條記錄。

您需要嘗試使用fetch_all方法來執行此操作。

同時查看您的條件。 如果user_id是唯一的,則由於您的where條件,它將僅返回單個記錄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM