有2个办法,第一种直接使用sql的多表联查,效率高,但是得到的数据table1会被扩展成table2一样的条目数 要再次处理
select * from table1 a,table2 b where a.orderid = b.orderid第二种方法,先得到table11的数据,在循环中匹配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已经废弃mysql方法了
while循环和foreach循环在mysql_fetch_array()的用法:while举例如下:
<?php
mysql_connect("localhost", "mysql_user", "mysql_password") or
die("Could not connect: " . mysql_error())
mysql_select_db("mydb")
$result = mysql_query("SELECT id, name FROM mytable")
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
printf ("ID: %s Name: %s", $row[0], $row[1])
}
mysql_free_result($result)
?>
foreach循环举例如下:
$rows = mysql_fetch_all($result)
foreach($rows as $row){
printf ("ID: %s Name: %s", $rows[0], $rows[1])
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)