PDO返回单个结果,如何从1列多行返回不同的记录 [英] PDO return single result, how to return different record from 1 column multiple rows
问题描述
我正在从我的数据库制作一个自定义Sitemap并坚持以下问题
SQL查询:
SELECT term_id FROM wp_term_taxonomy < span class =code-keyword> WHERE taxonomy = product-cat | | taxonomy = product-brand
查询输出
=========
term_id
=========
| 365 |
| 369 |
| 370 |
它返回我数据库中的所有term_id,通过它我从另一个表中收集类别slug来制作站点地图URL。下面是代码,它可以正常工作(站点地图输出),但不幸的是我无法提取下一行term_id &它显示总是与 term_id 相同的结果'相同的网址'
我的站点地图输出
< 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/schema s / sitemap / 0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\">
< span class =code-keyword>< url >
< loc >
http://domain.com/search-page/?product-cat=mobiles-tablets 365
< / loc >
< changefreq > 始终< / changefreq >
< priority > 1.0 < / priority >
< / url >
< url >
< loc >
http://domain.com/search-page/ ?product-cat = mobiles-tablets 365
< / loc >
< changefreq > 始终< / changefreq >
< priori ty > 1.0 < / priority >
< / url >
< / urlset >
我的代码
<?php
header( Content-type:text / xml);
$ i = 0;
$ xml = ' <?xml version =1.0encoding =UTF-8?>';
$ xml。= \ n。' < urlset xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexmlns:image =http:// www.google.com/schemas/sitemap-image/1.1xsi:schemaLocation =http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/ sitemap.xsdxmlns =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 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-品牌'跨度>);
$ t_id [$ i] = $ sth [$ i] - > fetchColumn();
$ stmt [$ i] = $ db-> query(' SELECT分类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>'。 \\\
跨度>;
$ xml。= \t\t\t。' < loc>'。 http://domain.com/search-page/?$t_taxonomy [$ i] = $ t_slug [$ i]。 \t $ t_id [$ i] \ t $ i \ t $ rowcount。 ' < / loc>';
$ xml。= \ n \\\\\\ 。' < changefreq> always< / changefreq>';
$ xml。= \ n \\\\\\ 。' < priority> 1.0< / priority>';
$ xml。= \ n\t\t。' < / url>'。 < span class =code-string> \ n;
}
?>
< ? php
$ xml。= \ n。' < / urlset>';
$ handle = fopen(' sitemap_custom。 xml',' w +');
fwrite($ handle,$ xml);
fclose($ handle);
?>
我有什么尝试过:
我需要什么?
我想要提取下一行term_id(理想情况,如果它基于循环[0,1,2])现在它只显示 term_id = 365
I = 0;
xml = ' <?xml version =1.0encoding =UTF-8?>';
xml。= \ n。' < urlset xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexmlns: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.xsdxmlns =http:// www。 sitemaps.org/schemas/sitemap/0.9\">'跨度>;
I am making a custom Sitemap from my database and stuck with following Issue
SQL Query:
SELECT term_id FROM wp_term_taxonomy WHERE taxonomy="product-cat" || taxonomy="product-brand"
Query Output
========= term_id ========= | 365 | | 369 | | 370 |
It returns all term_id from my database through which I gather category slugs from another table to make sitemap URLs. Below is the code and it works fine as you can see in (sitemap output), but unfortunately I am unable to extract next row term_id & it shows always same term_id to me as a result 'Same URL'
My Sitemap Output
<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://domain.com/search-page/?product-cat=mobiles-tablets 365
</loc>
<changefreq>always</changefreq>
<priority>1.0</priority>
</url>
<url>
<loc>
http://domain.com/search-page/?product-cat=mobiles-tablets 365
</loc>
<changefreq>always</changefreq>
<priority>1.0</priority>
</url>
</urlset>
MY CODE
<?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://domain.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);
?>
What I have tried:
What I Need?
I want to extract next row term_id (ideal if it based on loop[0,1,2]) right now it shows me only result of term_id=365
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">';
这篇关于PDO返回单个结果,如何从1列多行返回不同的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!