谁能帮我理解为什么会出现以下错误:
类 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/