php mysql 查询一个表 相同字段两次的一个问题
tb:tb_id,integer,notnulltb_content,varchar(50),notnull$string="selecttb1.tb_content,t...
tb :
tb_id, integer, not null
tb_content, varchar(50),not null
$string = "select tb1.tb_content, tb2.tb_content from tb as tb1, tb as tb2 where tb1.tb_id =1 and tb2.tb_id=4;";
$query = mysql_query($string, $db_connection);
while($array=mysql_fetch_array($query))
{
print("<br>".$array[tb1.tb_content]."<br>");
}
不输出
请问怎样解决? 展开
tb_id, integer, not null
tb_content, varchar(50),not null
$string = "select tb1.tb_content, tb2.tb_content from tb as tb1, tb as tb2 where tb1.tb_id =1 and tb2.tb_id=4;";
$query = mysql_query($string, $db_connection);
while($array=mysql_fetch_array($query))
{
print("<br>".$array[tb1.tb_content]."<br>");
}
不输出
请问怎样解决? 展开
2个回答
展开全部
首先你要确认数据库里面存在tb_id为1和4的记录。
其次你输出$array[tb1.tb_content]是不对的,两个字段都会叫做$array[tb_content],你可以用$array[0]、$array[1]来输出,或者语句里面定义别名:
$string = "select tb1.tb_content content1, tb2.tb_content content2 from tb as tb1, tb as tb2 where tb1.tb_id =1 and tb2.tb_id=4";
这样就可以输出$array[content1]、$array[content2]
另外最好程序中随时检测并输出错误信息,我修改语句如下:
if ($query = mysql_query($string, $db_connection)){
...while ...
}else echo "执行 $string 错误:".mysql_error();
其次你输出$array[tb1.tb_content]是不对的,两个字段都会叫做$array[tb_content],你可以用$array[0]、$array[1]来输出,或者语句里面定义别名:
$string = "select tb1.tb_content content1, tb2.tb_content content2 from tb as tb1, tb as tb2 where tb1.tb_id =1 and tb2.tb_id=4";
这样就可以输出$array[content1]、$array[content2]
另外最好程序中随时检测并输出错误信息,我修改语句如下:
if ($query = mysql_query($string, $db_connection)){
...while ...
}else echo "执行 $string 错误:".mysql_error();
展开全部
$string
=
"select
tb1.tb_content,
tb2.tb_content
from
tb
as
tb1,
tb
as
tb2
where
tb1.tb_id
=1
and
tb2.tb_id=4;";
print("<br>".$array[tb1.tb_content]."<br>");
修改成:$string
=
"select
tb1.tb_content
as
content1,
tb2.tb_content
as
content2
from
tb
as
tb1,
tb
as
tb2
where
tb1.tb_id
=1
and
tb2.tb_id=4";
print("<br>".$array['content1']."<br>");
print("<br>".$array[tb1.tb_content]."<br>");
这样打印是不对的,至少应该是print("<br>".$array['tb1.tb_content']."<br>");
=
"select
tb1.tb_content,
tb2.tb_content
from
tb
as
tb1,
tb
as
tb2
where
tb1.tb_id
=1
and
tb2.tb_id=4;";
print("<br>".$array[tb1.tb_content]."<br>");
修改成:$string
=
"select
tb1.tb_content
as
content1,
tb2.tb_content
as
content2
from
tb
as
tb1,
tb
as
tb2
where
tb1.tb_id
=1
and
tb2.tb_id=4";
print("<br>".$array['content1']."<br>");
print("<br>".$array[tb1.tb_content]."<br>");
这样打印是不对的,至少应该是print("<br>".$array['tb1.tb_content']."<br>");
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询