如何从多个表中选择数据并使用php显示?

时间:2013-11-11 18:35:53

标签: php mysql

<?php    
$dbhost = 'localhost'; 
$dbuser = 'root'; 
$dbpass = ''; 
$dbname = 'moviefone';   
$con = mysql_connect($dbhost, $dbuser, $dbpass); 
mysql_select_db($dbname, $con); 
// Check connection 
if (mysqli_connect_errno()) { 
echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
}    
$data = mysql_query(
"
SELECT * 
  FROM new_hindi 
  JOIN new_eng 
 ORDER 
    BY id DESC 
 LIMIT 4
 ") or die(mysql_error());    

 $info=array();    

 while ($row =mysql_fetch_array($data)){
    $info[]=$row;    }    

?>

我的问题是它只循环第一个表中的最后一个数据,而第二个表循环最后四个条目。我想要的是第一个表也循环最后四个条目。

2 个答案:

答案 0 :(得分:0)

虽然您使用的库已弃用,但不应用于新项目(http://us3.php.net/function.mysql-connect),但您的问题在于您的连接语句。您需要指示要加入表的SQL 如何。以下是一个通用示例:

SELECT `field1` FROM `table1` INNER JOIN `table2` ON 
`table1`.`id` = `table2`.`table1_id` WHERE etc...

干杯

答案 1 :(得分:0)

也许你正在追求UNION ......

$query = "
SELECT * 
  FROM new_hindi 
 UNION 
SELECT *
  FROM new_eng 
 ORDER 
    BY id DESC 
 LIMIT 4;
 ";

$result = mysqli_query($db,$query) or die(mysqli_error());    

$info=array();    

 while ($row =mysql_fetch_array($result)){
 $info[]=$row;    
}