我正在尝试为大型项目构建PDO查询,以获取SQL提取查询。这些SQL查询使用变量来获取值。我无法使用在教程中获得的内容-我不明白如何修改它们以使其能够使用变量而不是例如:id
。我有很多文件要更新为PDO,因为当前它们是使用mysql()
查询示例(数百个文件中的一个):
if(!$GLOBALS['this_brand']->id) return false;
if(!$GLOBALS['url_lang']) return false;
if(!$GLOBALS['this_region']->id) return false;
if(!$GLOBALS['this_type']->id) return false;
if(!$GLOBALS['this_user']->group) return false;
if(!$liveid = $GLOBALS['this_config']->get('config_live_id')) return false;
if(!$draftid = $GLOBALS['this_config']->get('config_draft_id')) return false;
if(!$archiveid = $GLOBALS['this_config']->get('config_archive_id')) return false;
if($GLOBALS['this_user']->is_admin()) {
$statussql = "AND (page.status = ".$liveid." OR page.status = ".$draftid." OR page.status = ".$archiveid.")";
}
else {
$statussql = "AND page.status = ".$liveid;
}
$the_statement = "SELECT * FROM page
LEFT JOIN _page_brand ON page.id = _page_brand.page_id
LEFT JOIN _page_language ON page.id = _page_language.page_id
LEFT JOIN _page_region ON page.id = _page_region.page_id
LEFT JOIN _page_type ON page.id = _page_type.page_id
LEFT JOIN _page_group ON page.id = _page_group.page_id
LEFT JOIN _page_section ON page.id = _page_section.page_id
WHERE _page_brand.brand_id = ".$GLOBALS['this_brand']->id."
AND _page_language.language_iso = '".$GLOBALS['url_lang']."'
AND _page_region.region_id = ".$GLOBALS['this_region']->id."
AND _page_type.type_id = ".$GLOBALS['this_type']->id."
AND _page_group.group_id = ".$GLOBALS['this_user']->group."
AND _page_section.section_id = ".$GLOBALS['this_section']->id."
".$statussql."
AND page.url = '".$this->url."'
ORDER BY page.status ASC, page.moddate DESC
LIMIT 1";
我的PDO尝试(看起来确实很me脚,但是我尝试了不同的方法,但是一旦它们不起作用就删除了代码):
public function fetchRaw($query){
$stmt = $this->db->query($query);
$arr = [];
foreach ($stmt as $row) {
$rarr = [];
foreach($row as $k=>$v) {
$rarr[$k] = $v;
}
$arr[] = $rarr;
}
return $arr;
}
我尝试了fetch()
,while
循环等。我无法去更改每个当前查询-有成千上万个查询。对此方法的任何帮助将不胜感激。我还不是PDO方面的专家,这个庞大的项目有些让人不知所措,但是我需要这样做。