有2個辦法,第一種直接使用sql的多表聯(lián)查,效率高,但是得到的數(shù)據(jù)table1會被擴展成table2一樣的條目數(shù) 要再次處理
我們提供的服務有:成都網(wǎng)站建設、網(wǎng)站設計、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、通道ssl等。為數(shù)千家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務,是有科學管理、有技術的通道網(wǎng)站制作公司
select?*?from?table1?a,table2?b?where?a.orderid?=?b.orderid
第二種方法,先得到table11的數(shù)據(jù),在循環(huán)中匹配table2到一個新的列名中
$conn?=?mysqli_connect("127.0.0.1",?"root",?"123",?"test");
$sql?=?"select?*?from?table1";
$rs?=?mysqli_query($conn,?$sql);
$Arr?=?array();
while?($row?=?mysqli_fetch_assoc($rs))?{
$sql?=?"select?*?from?table2?where?orderid?="?.$row["orderid"];
$row["order_sku"]?=?mysqli_fetch_all(mysqli_query($conn,?$sql),?MYSQLI_ASSOC);
$Arr[]?=?$row;
}
print_r($Arr)
如果你是剛開始學php 建議直接拋棄mysql用mysqli 因為PHP5.5已經(jīng)廢棄mysql方法了
php 把數(shù)據(jù)導出到excel表格有多種方法,比如使用 phpExcel 等,以下代碼是直接通過 header 生成 excel 文件的代碼示例:
?php
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:filename=xls_region.xls");
$cfg_dbhost?=?'localhost';
$cfg_dbname?=?'testdb';
$cfg_dbuser?=?'root';
$cfg_dbpwd?=?'root';
$cfg_db_language?=?'utf8';
//?END?配置
//鏈接數(shù)據(jù)庫
$link?=?mysql_connect($cfg_dbhost,$cfg_dbuser,$cfg_dbpwd);
mysql_select_db($cfg_dbname);
//選擇編碼
mysql_query("set?names?".$cfg_db_language);
//users表
$sql?=?"desc?users";
$res?=?mysql_query($sql);
echo?"tabletr";
//導出表頭(也就是表中擁有的字段)
while($row?=?mysql_fetch_array($res)){
$t_field[]?=?$row['Field'];?//Field中的F要大寫,否則沒有結果
echo?"th".$row['Field']."/th";
}
echo?"/tr";
//導出100條數(shù)據(jù)
$sql?=?"select?*?from?users?limit?100";
$res?=?mysql_query($sql);
while($row?=?mysql_fetch_array($res)){
echo?"tr";
foreach($t_field?as?$f_key){
echo?"td".$row[$f_key]."/td";
}
echo?"/tr";
}
echo?"/table";
?
用聯(lián)合查詢就可以實現(xiàn)
一般包括左外連接,右外連接和內連接
可以用on設置每兩個表之間的關聯(lián)關系,查詢后遍歷輸出到頁面就可以了
網(wǎng)站欄目:php遍歷數(shù)據(jù)到表格,用php循環(huán)出一個表格
URL鏈接:http://www.muchs.cn/article30/hcpjso.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、定制開發(fā)、品牌網(wǎng)站建設、微信公眾號、自適應網(wǎng)站、外貿網(wǎng)站建設
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)