简体   繁体   English

致命错误:无法将PDO类型的对象用作数组

[英]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输出变量的数据类型,内容和结构。

  1. Use the following to output the content in human readable format. 使用以下内容以可读格式输出内容。

     echo "<pre>"; print_r($variable); echo "</pre>"; die(); 
  2. Use the following to output the content with datatype and extra info 使用以下内容输出具有数据类型和额外信息的内容

     echo "<pre>"; var_dump($this); echo "</pre>"; die(); 
  3. Remember functions like 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.

相关问题 PDO致命错误:不能将PDOException类型的对象用作数组 - PDO Fatal error: Cannot use object of type PDOException as array 致命错误:无法将类型的对象用作数组 - Fatal Error : Cannot use object of type as array PDO错误无法将stdClass类型的对象用作数组 - PDO error Cannot use object of type stdClass as array PDO无法将广告类型的对象用作数组(格式错误) - PDO Cannot use object of type ads as array (form error) 致命错误:未被捕获的错误:不能将Entrada类型的对象用作数组 - Fatal error: Uncaught Error: Cannot use object of type Entrada as array FATAL ERROR未捕获错误:无法使用stdClass类型的对象作为数组 - FATAL ERROR Uncaught Error: Cannot use object of type stdClass as array 致命错误:未捕获错误:无法使用PDOStatement类型的对象作为数组 - Fatal error: Uncaught Error: Cannot use object of type PDOStatement as array 致命错误:不能将Stripe \\ Customer类型的对象用作数组 - Fatal error: Cannot use object of type Stripe\Customer as array 致命错误:无法在第33行中将stdClass类型的对象用作数组 - Fatal error: Cannot use object of type stdClass as array in line 33 致命错误:无法将类型为User \\ Entity \\ User的对象用作数组 - Fatal error: Cannot use object of type User\Entity\User as array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM