2012-11-27 71 views
1

我在mysql有兩個表格。以下是我的劇本。匹配兩個表中的數據如果匹配..顯示名稱和味精

<?php 
$conn=mysql_connect("localhost",'root',''); 
mysql_select_db('test'); 

$sql = "SELECT data_student.name,ozekimessagein.msg 
      FROM ozekimessagein,data_student 
      WHERE ozekimessagein.sender=data_student.sender 
      ORDER BY ozekimessagein.msg"; 

$res=mysql_query($sql); 
$cn=mysql_num_rows($res); 
for($x=0;$x<$cn;$x++) 
{ 
    list($name,$msg)=mysql_fetch_row($res); 
    echo "<li>$name,$msg"; 
} 
mysql_close($conn); 
?> 

本應顯示從表data_student表的名稱和從ozekimessagein只要data_student表匹配發送者與所述ozekimessagein表發送方的消息。但它不起作用。

+0

關閉主題:選中此[爲什麼不應該使用mysql_ *函數在PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt -i-use-mysql-function-in-php) – nu6A

回答

0

試試這個::

SELECT 

data_student.name, 
ozekimessagein.msg 

FROM ozekimessagein 
inner join data_student  on ozekimessagein.sender=data_student.sender 

ORDER BY ozekimessagein.msg 
+0

我已經嘗試過這樣的代碼..它不起作用。 。它顯示例如**名稱**:消息。但只要ozekimessagein表中的發件人與data_student表中的發件人匹配,此參數將從不同的表中獲取。 –

0

試試這個

<?php 
    $conn=mysql_connect('localhost','root',''); 
    mysql_select_db('test'); 

$sql = " 

    SELECT s.name AS names, o.msg AS msgs 
    FROM ozekimessagein o 
    INNER JOIN data_student s 
    ON o.sender = s.sender 
    ORDER BY s.name"; 

    $res=mysql_query($sql); 

    while ($row = mysql_fetch_assoc($res)) { 

echo "<li>".$row['names']." : ".$row['msgs']."</li>"; 
    } 
    mysql_close($conn); 
    ?> 
+0

現在更新,這應該適合你 –

+0

我確實嘗試了你的代碼。但不幸的是瀏覽器上沒有顯示任何內容。 –

+0

試着讓這個在上面,看看你得到了什麼錯誤'ini_set('display_errors',1); error_reporting(E_ALL);' –