select *
from `ugm_show_prod`
where `enable`='1'
```
別名查詢 ```
select a.*
from `ugm_show_prod` as a
where a.`enable`='1'
```
選擇欄位 ```
select a.`sn`,a.`title`,a.`summary`
from `ugm_show_prod` as a
where a.`enable`='1'
```
跨表查詢 ```
select a.`sn`,a.`title`,a.`summary`,b.`file_name`,b.`sub_dir`
from `ugm_show_prod` as a
left join `ugm_show_files_center` as b on a.sn=b.col_sn and b.col_name='prod'
where a.`enable`='1'
```
![](https://www.ugm.com.tw/uploads/tad_book3/image/ugm_show/prod_8.jpg)
得到上面查詢語法,可以製做成一個函數,放在index.php ```
###########################################################
# 撈商品資料
###########################################################
function get_prod() {
global $db, $smarty;
#撈商品資料
$sql = "select a.`sn`,a.`title`,a.`summary`,b.`file_name`,b.`sub_dir`
from `ugm_show_prod` as a
left join `ugm_show_files_center` as b on a.sn=b.col_sn and b.col_name='prod'
where a.`enable`='1'
order by a.`sort` desc
";//die($sql);
$result = $db->query($sql) or redirect_header("", 3000, $db->error."\n".$sql,true);
$rows=[];
while($row = $result->fetch_assoc()){
#過濾資料
$row['sn'] = intval($row['sn']);
$row['title'] = htmlspecialchars($row['title'], ENT_QUOTES);
$row['summary'] = htmlspecialchars($row['summary'], ENT_QUOTES);
$row['file_name'] = htmlspecialchars($row['file_name'], ENT_QUOTES);
$row['sub_dir'] = htmlspecialchars($row['sub_dir'], ENT_QUOTES);
$row['pic'] = "";
if($row['file_name']){
$row['pic'] = WEB_URL."/uploads/{$row['sub_dir']}/" . $row['file_name'];
}
$rows[] = $row;
}
//print_r($rows);die();
$smarty->assign("prods", $rows);//送至樣板
}
```
更改樣板 ```
<{foreach from=$prods item=row}>
<{/foreach}>
```