‘壹’ php如何关联两个或者多个数据表
至少三个方法可以实现:
一、使用视图来实现多表联合查询,
例如:创建视图:create view userstoposts as select u.name,u.qq,p.post_id,p.title, p.contents, p.contents from users as u,posts as p where u.name=p.name
二、直接使用表联合查询
例如:select u.name,u.qq,p.* from users as u,posts as p where u.name=p.name
三、结合PHP语言实现
例:1、
<?php
$Sql="select *from posts";
$Result=@mysql_query($Sql);
while($rows=mysql_fetch_assoc($Result)){
$sql1="select name,qq from users where name='".$rows['name']."'";
$result1=@mysql_query($sql1);
$rows1=mysql_fetch_assoc($result1);
$OUTPUT[]=array(
'name'=>$rows['name'],
'qq'=>$rows1['qq'],
'post_id'=>$rows['post_id'],
'title'=>$rows['title'],
'contents'=>$rows['contents']
);
}
print_r($OUTPUT);//可以你需要的结果输出
?>