包含某些列时,PHP PDO Fetch返回0结果

时间:2017-05-06 18:51:51

标签: php mysql

我创建了与数据库的连接,调用fetch并在json中回显我的结果;但是,当我在select语句中包含某些列时......什么都没有回来。

这是我的代码的外观:

$data = array();
$page = 0;
$perPage = 30;
$offset = $page * $perPage;

$query = "SELECT  wp.ID, 
          REPLACE(wp.post_title, 'Episode - ','') AS header, 
          CASE WHEN img.guid IS NULL THEN meta_img.meta_value ELSE img.guid END AS image_url,
          wp.post_excerpt AS about,  **INCLUDING THIS**
          lessons.meta_value as lessons,  **OR THIS**
          title.meta_value AS detail **OR THIS. BRINGS BACK NO RESULTS**
          FROM wp_posts wp
          INNER JOIN wp_postmeta title on title.post_id = wp.id and title.meta_key = 'academy_title'
          INNER JOIN wp_postmeta lessons on lessons.post_id = wp.id and lessons.meta_key = 'academy_lessons'
          LEFT JOIN wp_postmeta meta_img on meta_img.post_id = wp.id and meta_img.meta_key = 'image'
          LEFT JOIN wp_postmeta meta on meta.post_id = wp.id and meta.meta_key = '_thumbnail_id'
          LEFT JOIN wp_posts img on img.id = meta.meta_value
          WHERE wp.post_title LIKE 'Episode%' AND wp.post_status = 'publish' 
          ORDER BY wp.post_date DESC
          LIMIT $offset, $perPage";

        $result = $this->db->query($query);

         while ($row = $result->fetch()) {
                $data[] = $row;
        }    
        echo json_encode($data);

我知道我的连接工作正常,因为其他更简单的查询有效,但出于某种原因,当我在查询(about, lessons, detail)中包含最后3列时,结果没有带来任何回复......这没有任何意义

我对此有点新意,所以我想也许某些语法不被接受?但问题是,它不会返回基本列的结果,而不是具有实际逻辑的列...

编辑:

在对SUBSTR(wp.post_excerpt, 1, 4) as about字段进行操作并限制其大小时...可行。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

检查phpAdmin或其他查询,确保这些列存在且写得很好。

更新尝试

 $data = array();      
 while ($row = $result->fetch()) {
                $data[] = $row;
        }   

更新2 也许有关于编纂的问题,特别是类似的问题