2017-06-20 96 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]);' –