从数据库PHP OOP返回结果


Return results from database PHP OOP

我还在学习PHP OOP和英语
我不知道如何从数据库返回结果。我编写了返回结果的方法:

class Serial extends DbUser {
public $history, $sn, $added, $numrows;
function __construct() {
    parent::__construct('localhost', 'michal', '', 'test');
}
function historyProduct($sn) {
    $result = $this->history = $this->conn->prepare("SELECT * FROM `products` WHERE sn = ?");
    $result = $this->history->bind_param('s', $sn);
    $result = $this->history->execute();
    $result = $this->history->get_result();
    return $result;
    while ($row = mysqli_fetch_array($result)) {
        $this->sn = $row['sn'];
        $this->added = $row['added'];
    }
    $this->numrows = mysqli_num_rows($result);
}
function __toString() {
    return (string) $this->numrows;
}
}
$sn = $_GET['sn'];
$history = new Serial();
$history->historyProduct($sn);
printf($history);
echo $history->added;

但是这个方法没有显示任何内容。

我试过这样做:php oop类的返回值
我做错了什么?

米夏乌。

php函数只会处理到返回语句。例如,这将具有与"break"相同的效果。除了它返回值之外。

如果希望执行while子句,则必须将return语句放在方法的最后。

为了更好的可读性,我建议您设置一次结果值,而不是每次$this->history方法调用。