[英]Fatal error: Cannot use object of type PDO as array
Here is the error Log 这是错误日志
Fatal error: Cannot use object of type PDO as array in /Applications/XAMPP/xamppfiles/htdocs/php/blog/single.php on line 13
致命错误:无法在第13行的/Applications/XAMPP/xamppfiles/htdocs/php/blog/single.php中将PDO类型的对象用作数组
Here is 13 number line 这是13号线
$post = DB\query('SELECT * FROM posts WHERE id = :id LIMIT 1', array('id' => $_GET['id']), $conn [0] );
i got this error when i try to get post title. 当我尝试获取帖子标题时出现此错误。
<?= $post['title'];?>
Full Code 完整代码
<?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';
Instead of $conn[0]
, Try using $conn
. 尝试使用
$conn
代替$conn[0]
。
For future issues, always remember you can output the datatypes, content, and structures of variables in php
. 对于将来的问题,请始终记住可以在
php
输出变量的数据类型,内容和结构。
Use the following to output the content in human readable format. 使用以下内容以可读格式输出内容。
echo "<pre>"; print_r($variable); echo "</pre>"; die();
Use the following to output the content with datatype and extra info 使用以下内容输出具有数据类型和额外信息的内容
echo "<pre>"; var_dump($this); echo "</pre>"; die();
gettype()
etc. gettype()
类的函数。 Also based on your further comments, I'd recommend that you first grab a book or an online course of the language. 另外,根据您的进一步评论,我建议您首先阅读一本书或该语言的在线课程。
About your next error, remember that in php
the variable needs to be defined before you can call/use it. 关于下一个错误,请记住,在
php
中需要先定义变量,然后才能调用/使用它。
So on the line, where you care calling $post['title'];
因此,在网上,您需要在其中调用
$post['title'];
remember to first make sure that that variable is defined and has the index
that you intend to call. 请记住首先确保已定义该变量并具有您要调用的
index
。 Also use the above snippets to verify and you should be writing the handling code if that index is not set. 还要使用上面的代码片段进行验证,如果未设置该索引,则应该编写处理代码。
something like.. 就像是..
if(isset($post) && !empty($post) && isset($post['title'])) {
....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.