dou47278 2019-05-03 12:29
浏览 298
已采纳

如何从mySQL数据库中获取时间戳?

my query won't show a timestamp if i call the function getDate_created();. how can i fix this? The function getAll() does work.

<?php

class Post
{
    public $date_created;


    public function getDate_created()
    {
        return $this->date_created;
    }

    public function setDate_created($date_created)
    {
        $this->date_created = $date_created;

        return $this;
    }

    public static function getAll()
    {
        $conn = Db::getInstance();
        $result = $conn->query('SELECT * FROM images');

        return $result->fetchAll(PDO::FETCH_CLASS, __CLASS__);
     }
 }


$posts = Post::getAll();

$timepost = new Post();
$t = $timepost->getDate_created();
var_dump($t);

the table images:

CREATE TABLE `images` (
   `id` int(11) NOT NULL,
   `user_id` int(11) NOT NULL,
   `img_description` varchar(255) NOT NULL,
   `date_created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
   `file_path` varchar(255) NOT NULL
   ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Var_dump($t) shows NULL as result.

  • 写回答

2条回答 默认 最新

  • douxun1934 2019-05-03 12:50
    关注

    You are creating a new instance of Post when you do $timepost = new Post();

    If you want to read one of those posts from database you should read one (or more) from those that came from getAll() method.

    Ex:

    $posts = Post::getAll();
    
    foreach($posts as $post) {
        var_dump($post->getDate_created());
    }
    
    //or
    
    var_dump($posts[0]->getDate_created());
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决