我創建了一個到數據庫的連接,調用fetch並獲取我的結果在json中回顯;但是,當我在select語句中包含某些列時......什麼也沒有回來。PHP PDO Fetch在包含某些列時返回0結果
這是我的代碼的外觀:
$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);
我知道我的連接工作正常,因爲其他簡單的查詢工作,但由於某種原因,當我在查詢中包含的最後3列(about, lessons, detail)
結果帶來任何回報...這是沒有意義的
我有點新這個,所以我想也許有些語法不被接受?但事實是,它不返回基本列結果不符合實際邏輯的...
編輯:
在做SUBSTR(wp.post_excerpt, 1, 4) as about
與領域,並限制其規模...它的工作原理。我該如何解決?
變化'lessons.meta_value的教訓,''來作爲lessons.meta_value my_lessons, '並嘗試。同時檢查'var_dump($ result);' –
@AlivetoDie我已經嘗試更改別名,因爲他們被保護的關鍵字,並沒有工作。 var_dump($ result)在while循環中返回查詢...但var_dump($ row)返回所有結果... – Walker
'var_dump($ row)'有數據嗎?那麼問題是什麼? 'echo json_encode($ data);'不打印任何東西? –