简体   繁体   中英

Return results from database PHP OOP

I am still learning PHP OOP and english too.
I do not know how I return result from database. I wrote method, which returns results:

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;

But this method does not display anything.

I have tried with this: return values from class php oop
What I am doing wrong?

Michał.

A php function will only process until it reaches a return statement. This would have the same effect as "break" for example. Except that it returns the value.

If you want your while clause to be executed you have to place the return statement at the very end of your method.

Also for better readability I would suggest that you set the result value once and not for every $this->history method call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM