php mysql 查询一个表 相同字段两次的一个问题

来源:百度知道 编辑:UC知道 时间:2024/05/19 21:47:06
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为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();