2016-02-16 140 views
1

的1号线所以基本上我有一个数据库表:PHP类只返回结果

ID,USER_ID,职业,年,HIGHEST_POSITION

我创建了一个类:

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 [ocupation] =>忍[岁] => 100 [HIGHEST_POSITION] =>黑带)

但是,这只是第一个结果,我如何得到其他结果?

欢呼声, 丹

+1

移除/更改查询中的WHERE子句,以便获得所有结果;或者拨打电话并使用你的'id'作为数组。 – rhavendc

+0

再次阅读你的sql查询,特别关注'WHERE'子句。如果您在工作台或phpmyadmin中执行此查询,会返回什么结果 – mumair

回答

1

$WorkHistoryQuery->fetch();只会除非你while循环使用返回一条记录。

你需要尝试一个fetch_all方法来做到这一点。

也看看你的条件。如果user_id是唯一的,那么它将仅返回单个记录,因为您的where条件。