【发布时间】:2016-06-22 16:32:47
【问题描述】:
这是错误日志
致命错误:无法在第 13 行的 /Applications/XAMPP/xamppfiles/htdocs/php/blog/single.php 中使用 PDO 类型的对象作为数组
这是13号线
$post = DB\query('SELECT * FROM posts WHERE id = :id LIMIT 1', array('id' => $_GET['id']), $conn [0] );
我在尝试获取帖子标题时遇到此错误。
<?= $post['title'];?>
完整代码
<?php
require 'functions.php';
use blog\DB;
// Connect to the DB
$conn = DB\connect($config);
if( !$conn ) die('Problem Connecting to the DB');
// Fetch all the posts
$post = DB\query('SELECT * FROM posts WHERE id = :id LIMIT 1', array('id' => $_GET['id']), $conn [0] );
// Filter throgh and display in the view
$view_path = 'views/single.view.php';
include 'views/layout.php';
【问题讨论】:
-
你能发布你的数据库类吗?此外,您没有正确调用类方法。
-
将 $conn[0] 更改为 $conn
-
$post = DB\query('SELECT * FROM posts WHERE id = :id LIMIT 1', array('id' => $_GET['id']), $conn );
-
现在出现此错误注意:未定义索引:第 1 行 /Applications/XAMPP/xamppfiles/htdocs/php/blog/views/single.view.php 中的标题 = $post['title '];?>