php - 错误 : Object of class stdClass could not be converted to string -- implode() -- php

标签 php mysql wordpress implode

谁能帮我理解为什么会出现以下错误:

类 stdClass 的对象无法转换为字符串...(错误指向带有 implode() 的行, 见下文)

当我运行以下函数时?

  function selectFullArticle () {

    global $wpdb;

  $id=get_the_ID();


      $webPageArticle = $wpdb->get_results( "SELECT post_content_long FROM $wpdb->posts WHERE ID=$id" );

 $webPageArticle= implode(" ",$webPageArticle);
 return $webPageArticle;

我的目标是返回一个字符串而不是一个数组。

也许必须以不同的方式处理从 SELECT 返回的数组?

提前致谢

码头


感谢您的回答。我正在尝试显示我从网上下载并保存在 wordpress 数据库中的网页,而不是帖子。

两者都有 $webPageArticle = $wpdb->get_results( "SELECT post_content_long FROM $wpdb->posts WHERE ID=$id",ARRAY_N); 和 $webPageArticle = $wpdb->get_results( "SELECT post_content_long FROM $wpdb->posts WHERE ID=$id",ARRAY_A);

运行良好并且 implode() 不再提示。但是,我没有得到真正的字符串,因为语句“echo $webPageArticle;”在屏幕上可视化单词“Array”。

怎么会呢?

码头

最佳答案

Codex 中所读,您可以将额外的第二个参数传递给 get_result() 以允许它返回数组而不是对象

 <?php $wpdb->get_results('query',OBJECT_K); ?> 

返回一个你可以操作的关联数组。

引用:

OBJECT - result will be output as a numerically indexed array of row objects.
OBJECT_K - result will be output as an associative array of row objects, using first column's values as keys (duplicates will be discarded).
ARRAY_A - result will be output as an numerically indexed array of associative arrays, using column names as keys.
ARRAY_N - result will be output as a numerically indexed array of numerically indexed arrays

关于php - 错误 : Object of class stdClass could not be converted to string -- implode() -- php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7724311/

相关文章:

php - Mysql在不使用别名的情况下连接具有相同列名的2个表

php - 匹配和删除字符串中的推特主题标签

php - 仅显示产品分类的第一个和最后一个术语

wordpress - WooCommerce 注册短代码 - 错误消息问题

mysql - Wordpress - 建立数据库连接时出错

php - 编辑配置文件 joomla 中禁用的字段名称

php - 位置排序不适用于产品列表页面 magento2

javascript - 从不同的表中选择并组织相同的列

php - 自动递增跳过 ids

php - 从多个用户的两个表中减去两个分数。 mysql