gpt4 book ai didi

php 类只返回第一行结果

转载 作者:行者123 更新时间:2023-11-28 23:37:16 25 4
gpt4 key购买 nike

所以基本上我有一个数据库表:

id, user_id, occupation, years, 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;
}
}

然后我通过执行以下操作来拉动类(class):

$wh = new WorkHistory($logged_user);

当我这样做时:

print_r($wh);

它返回:

WorkHistory 对象 ( [id] => 3 [user_id] => 3 [ocupation] => Ninja [years] => 100 [highest_position] => Black Belt )

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

干杯,担

最佳答案

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

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

同时查看您的 where 条件。如果 user_id 是唯一的,那么由于您的 where 条件,它将只返回一条记录。

关于php 类只返回第一行结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35424932/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com