0

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ł.

1
  • The line return $result; in your method pretty much ensures that nothing will be set for the added and the numrows properties of the class; Commented May 16, 2016 at 18:23

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.