从表中选择一个值并将其传递给变量 - php mysql

时间:2016-08-17 13:04:41

标签: php mysql pdo

我想创建名为$start的变量。作为一个值,我想从名为timestamp的表的最后一行中选择一个名为table_ex的列中的值。到目前为止,我有这个:

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;
}
}

问题是变量没有得到我想要的那个值。谁能解释一下我的问题在哪里?

1 个答案:

答案 0 :(得分:1)

也许这对你有用:

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);
}