从数据库PHP OOP返回结果

时间:2016-05-16 18:13:37

标签: php return

我还在学习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;

但是这种方法不会显示任何内容。

我试过这个: return values from class php oop
我做错了什么?

米哈尔。

1 个答案:

答案 0 :(得分:0)

php函数只会在到达return语句之前处理。例如,这与“休息”具有相同的效果。除了它返回值。

如果你想要执行你的while子句,你必须将return语句放在方法的最后。

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