如何将php PDO fetchobject()方法与sqlite一起使用来获取主键字段?

如何将php PDO fetchobject()方法与sqlite一起使用来获取主键字段?,php,database,sqlite,pdo,Php,Database,Sqlite,Pdo,。。。。 我的整个index.php。这应该是一个微不足道的例子 sqlite> .mode line sqlite> select id,title, content from posts; id = title = title content = content in here id = title = a new title content = new content 我的问题是$post->id为空,但根据fetchObject的文档,应该

。。。。 我的整个index.php。这应该是一个微不足道的例子

sqlite> .mode line
sqlite> select id,title, content from posts;
     id = 
  title = title
content = content in here

     id = 
  title = a new title
content = new content
我的问题是$post->id为空,但根据fetchObject的文档,应该已映射到$post变量

我真的只是试图获取我正在显示的行的主键,以便添加一个删除链接

请注意,我可以毫无问题地打开、读取和写入数据库。标题和内容也被正确地检索到,但我只是停留在从fetchObject返回的对象获取id字段上

sqlite似乎永远不会在表的id字段中放入任何内容


更新:答案可能一直都是。这实际上是一个sqlite问题,而不是php问题。

告诉我们从何处获得$postsOk。我更新了帖子,以包括我如何分配$posts变量。请不要捕获仅在die中使用它的异常。为什么不让异常冒泡出来呢?我正在尝试一个简单的例子,当我的基础工作正常时,我会从die开始更新它。所以,如果知道为什么没有返回id,我甚至尝试使用fetchPDO::FETCH_ASSOC而不是fetchObject来获取它,然后在这里打印关联数组结果:id=>title=>title content=>content,我开始认为这比php更与sqlite相关。
sqlite> .mode line
sqlite> select id,title, content from posts;
     id = 
  title = title
content = content in here

     id = 
  title = a new title
content = new content
   // Insert a post into the DB
if (isset($_POST['title']) && isset($_POST['content'])) {
   try {
         // Fill in the values
         $title = $_POST['title'];
         $content = $_POST['content'];

         # echo "title=".$title.", content=".$content;   

         // Create a prepared statement
         $stmt = $db->prepare("INSERT INTO posts (title, content) VALUES (:title, :content);");
         $stmt->bindParam(':title', $title);
         $stmt->bindParam(':content', $content);

         $stmt->execute();
      } catch (Exception $e) {
         die ($e);
   }
}

// Get posts from database
try {
   $posts = $db->prepare('SELECT id, title, content FROM posts;');
   $posts->execute();
} catch (Exception $e) {
   die ($e);
}

?>

<h1>Posts - SQLite Example</h1>

<?php while ($post = $posts->fetchObject() ): ?>
<br/>     
<h2><?php echo $post->title ?></h2>
<?php echo $post->content ?>
<a href="<?php echo $_SERVER['PHP_SELF']; ?>?action=delete&id=<?php echo $post->id ?>"><br/>Delete Post</a>
<?php endwhile; ?>

<hr />

<h2>Add new Post</h2>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

<p>
<label for="title">Title:</label>
<input type="text" name="title" />
</p>

<p>
<textarea name="content" rows="8" cols="50"></textarea>