2015-09-02 21 views
0
<?php 
    include ('conn_db.php'); 
    mysql_connect("$servername", "$username", "$password")or die("cannot  connect"); 
    mysql_select_db("$db")or die("cannot select DB"); 
    /* Fetch first row */ 
    $sql="SELECT * FROM `monthly_payment` GROUP BY `card`"; //First row 
    $result=mysql_query($sql); 
    if($result === FALSE) { 
     die(mysql_error()); // TODO: better error handling 
    } 
    $pays = array(); 
    while($row=mysql_fetch_array($result,MYSQL_ASSOC)) 
    { 
    $pays[]=$row['card']; 
    } 

/* Fetch second one */ 

$sql2="SELECT * FROM `users`"; // Second row 
$result2=mysql_query($sql2); 

if($result2 === FALSE) { 
    die(mysql_error()); // TODO: better error handling 
} 
$usrs = array(); 
while($row2=mysql_fetch_array($result2,MYSQL_ASSOC)) 
{ 
$usrs[]=$row2['card']; 
} 
$newArray = array_diff($usrs, $pays);// To make difference 
print_r($newArray); 
?> 

在這段代碼的輸出是這樣的:如何在PHP中單獨輸出表列值?

Array ([3] => RN166 [4] => RN100) 

每當我想打印使用「回聲」輸出,也有出現錯誤消息。我想看到這樣的輸出:

RN166,RN100 

使用「回聲」。所以我可以使用foreach循環從數據庫中獲取所有字段。

monthly_payment

id | card 
1 | RN122 
2 | DD13 
3 | RN155 
4 | RN155 

用戶

id | card 
----------- 
1 | RN122 
2 | DD13 
3 | RN155 
4 | RN166 
5 | RN100 
+0

您的預期結果是什麼? – sAcH

+0

你收到什麼錯誤信息? – halfer

回答

1

你必須使用implode功能:

echo implode(',', $newArray); 
+0

感謝馬里奧...這段代碼現在工作很棒... – user2980415

1

使用破滅功能這可能對您有幫助

echo implode(',', $newArray);