K. Zakrzewski
K. Zakrzewski

Reputation: 59

Select one value from table and pass it to variable - php mysql

I want to create variable called $start. As a value I want to select one value from column called timestamp from the last row of my table called table_ex. So far I have this:

class Main {
//some other code

function dataBaseConnect(){
           //well working part
        }

function getTimeValue(){
        $sql = "SELECT `timestamp` FROM `table_ex` WHERE id=(SELECT MAX(id) FROM `table_ex`)";
    $this->start = $this->handler->query($sql, PDO::FETCH_COLUMN, 0);
    }

function printVal(){
        $this->dataBaseConnect();
        $this->getTimeValue();
        $this->messOuput = "Sth text " .$this->start;
}
}

The problem is that variable is not getting that value I wanted. Could anyone explain me where is the problem?

Upvotes: 1

Views: 258

Answers (1)

Rafal Kozlowski
Rafal Kozlowski

Reputation: 760

Maybe this will work for You:

function getTimeValue()
{
    // note the table name is now used in the inner query
    $sql = "SELECT `timestamp` FROM `table_ex` WHERE id=(SELECT MAX(id) FROM `table_ex`)";
    $this-start = $this->handler->query($sql, PDO::FETCH_COLUMN, 0);
}

Upvotes: 1

Related Questions