2017-06-20 99 views
1

我從我的數據庫,使您的自定義地圖,並堅持了以下問題PDO返回單一的結果,如何從1列多行

SQL查詢返回不同的記錄:

SELECT term_id FROM wp_term_taxonomy WHERE taxonomy="product-cat" || taxonomy="product-brand" 

查詢輸出

========= 
term_id 
========= 
| 365 | 
| 369 | 
| 370 | 

它返回所有term_id從我的數據庫,通過它時I g ather類別從另一個表中刪除,以製作網站地圖網址。下面是代碼,它工作正常,你可以看到在(站點地圖輸出),但不幸的是我無法提取下一行term_id &它總是顯示相同term_id給我結果「同一URL」

我的Sitemap輸出

<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"> 
<url> 
<loc> 
http://example.com/search-page/?product-cat=mobiles-tablets 365 
</loc> 
<changefreq>always</changefreq> 
<priority>1.0</priority> 
</url> 
<url> 
<loc> 
http://example.com/search-page/?product-cat=mobiles-tablets 365 
</loc> 
<changefreq>always</changefreq> 
<priority>1.0</priority> 
</url> 
</urlset> 

我的代碼

<?php 
header("Content-type: text/xml"); 
$i=0; 
$xml = '<?xml version="1.0" encoding="UTF-8"?>'; 
$xml.= "\n".'<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'; 
    $db = new PDO('mysql:host=xxx;dbname=xxx', 'xx', 'xxx'); 
    $stmt[$i] = $db->query('SELECT count(*) FROM wp_term_taxonomy WHERE taxonomy="product-cat" || taxonomy="product-brand"'); 
    $rowcount = $stmt[$i]->fetchColumn(); 
     for ($i=0; $i<2; $i++) 
     { 
     $sth[$i] = $db->query('SELECT term_id FROM wp_term_taxonomy WHERE taxonomy="product-cat" || taxonomy="product-brand"'); 
     $t_id[$i] = $sth[$i]->fetchColumn(); 
     $stmt[$i] = $db->query('SELECT taxonomy FROM wp_term_taxonomy WHERE term_id = '.$t_id[$i].''); 
     $t_taxonomy[$i] = $stmt[$i]->fetchColumn(); 
     $stmt[$i] = $db->query('SELECT slug FROM wp_terms WHERE term_id = '.$t_id[$i].''); 
     $t_slug[$i] = $stmt[$i]->fetchColumn(); 
     } 
     echo $xml; 
    for ($i=0; $i<2; $i++) 

     { 

     $xml.= "\n\t\t".'<url>'."\n"; 
     $xml.= "\t\t\t".'<loc>'."http://example.com/search-page/?$t_taxonomy[$i]=$t_slug[$i]"."\t$t_id[$i]\t$i\t$rowcount".'</loc>'; 
     $xml.= "\n\t\t\t".'<changefreq>always</changefreq>'; 
     $xml.= "\n\t\t\t".'<priority>1.0</priority>'; 
     $xml.= "\n\t\t".'</url>'."\n"; 
     } 
    ?> 


<?php 
$xml.= "\n".'</urlset>'; 
$handle = fopen('sitemap_custom.xml','w+'); 
fwrite($handle,$xml); 
fclose($handle); 
?> 

我需要什麼?

我想提取下一行term_id(如果它基於循環理想[0,1,2]),現在它表明我只是結果term_id = 365

+2

爲什麼不使用'$ stmt-> fetchAll()'?這樣你可以將所有的查詢結果都存入一個數組中,然後你只需遍歷該數組並執行你的代碼即可。 – FMashiro

回答

0

您將要使用的fetchAll()來抓取所有記錄。要獲取單個物體,您可以使用Fetch()

請參閱下面的示例。

$sth = $dbh->prepare("SELECT name, colour FROM fruit"); 
$sth->execute(); 

/* Fetch all of the remaining rows in the result set */ 
print("Fetch all of the remaining rows in the result set:\n"); 
$result = $sth->fetchAll(); 
print_r($result); 
+0

你好邊疆區, 立即添加此後,可看到導致這樣 '陣列 ( [0] =>數組 ( [term_id] => 365 [0] => 365 ) [ 1] =>數組 ( [term_id] => 369 [0] => 369 ) [2] =>數組 ( [term_id] => 370 [0] => 370 )' 但我該如何選擇每個** term_id **使用循環這裏是我的代碼 –

+0

**我的代碼** '$ sth [$ i] = $ db-> prepare(「SELECT term_id FROM wp_term_taxonomy WHERE taxonomy = '產品貓'|| (); $ sth [$ i] - > execute(); $ result [$ i] = $ sth [$ i] - > fetchAll(); print_r($ result [$ I]);' –