2015-07-19 104 views
-3

我正在一個PHP腳本,將獲得所需的信息,並在XML中顯示它。出於某種原因,我的布林不工作,我不知道如何做查詢。數據庫有兩個表,我想從中提取信息。兩張表都有一個共同點,就是'id_member'。 Themes表中有一堆垃圾 - 所以它有4個coulms - 'id_member','id_theme','varible'和'value'。 'varible'中有兩項我想過濾(顯示它們而不是其餘)「cust_armaus」和「cust_armamo」,然後value會顯示當然的值。因此,在主題表中,它將不止一次列出id_member以顯示不同的變量。這是我得到的是不工作對我來說:PHP的SQL查詢過濾器+

<?php 
header('Content-Type: text/xml'); 
$username="database_username"; 
$password="database_password"; 
$database="database_name"; 
mysql_connect('localhost',$username,$password); 
@mysql_select_db($database) or die("Unable to select database"); 

echo "<?xml version=\"1.0\"?>\n". 
    "<!DOCTYPE squad SYSTEM \"squad.dtd\">\n". 
    "<?xml-stylesheet href=\"squad.xsl?\" type=\"text/xsl\"?>\n"; 
?> 
<squad nick="Team Tag"> 
    <name>Team Name</name> 
    <email>[email protected]</email> 
    <web>http://www.team.com</web> 
    <picture>teamlogo.paa</picture> 
    <title>This is the Team motto</title> 
<?php 

// smf_themes -> id_member = 
// smf_themes -> variable for cust_armaus & cust_usermo 
// smf_themes -> value <- get for cust_armaus & cust_usermo 
// smf_members -> real_name = profile_name & name 
// smf_members -> email_address = email - NO 

$memberSQL = mysql_query("SELECT id_member AS id, real_name FROM smf_members"); 
$member = mysql_fetch_array($memberSQL); 
$armausSQL = mysql_query("SELECT id_member, variable, value AS arma_id FROM smf_themes WHERE id_member='$member[id]' AND variable='cust_armaus'"); 
$armaid = mysql_fetch_array($armausSQL); 
$armamoSQL = mysql_query("SELECT id_member, variable, value AS motto FROM smf_themes WHERE id_member='$member[id]' AND variable='cust_usermo'"); 
$armamo = mysql_fetch_array($armamoSQL); 

    $num=mysql_numrows($member, $armaid, $armamo); 
    mysql_close(); 
    $i=0; 

    while ($i < $num) { 
    $profile_id = mysql_result($armaid,$i,"value"); 
    $profile_name = mysql_result($member,$i,"real_name"); 
    $profile_remark = mysql_result($armamo,$i,"value"); 
    $profile_username = mysql_result($member,$i,"real_name"); 
    //$profile_email = mysql_result($result,$i,"email"); 
    //$profile_icq = mysql_result($result,$i,"icq"); 

    echo "<member id=\"$profile_id\" nick=\"$profile_name\">" . 
     "<name>$profile_name</name>". 
     "<email>$profile_email</email>". 
     "<icq>$profile_icq</icq>". 
     "<remark>$profile_remark</remark>". 
    "</member>\n"; 

    $i++; 
    } 
?> 
</squad> 
+0

一件事,你過早地關閉您的連接'mysql_close();' –

+0

再加上,我認爲這是一個自定義函數'MYS ql_numrows()'?如果沒有,則缺少下劃線。 –

+0

對於單個答案,這裏有太多的錯誤。除了關於過早調用'mysql_close'的註釋以及帶有三個參數的神祕'mysql_numrows'函數之外''smf_themes''的查詢僅對從'smf_members'返回的第一行運行。 'while'循環不應該在'$ i'上,它應該是'while($ row = ... fetch ...())'循環。除了所有這些問題之外,'mysql'接口已被棄用;新開發應該使用'mysqli'或'PDO'。 – spencer7593

回答

0

mysql接口已被棄用。新開發應使用mysqliPDO

假設你只有一個一個的cust_armauscust_usermo行的值爲每個smf_members,你可以在選擇列表中使用相關子查詢,並返回只是返回單行在smf_members每行一個查詢。事情是這樣的:

SELECT m.id_member AS id 
     , m.real_name 
     , (SELECT a.value 
      FROM smf_themes a 
      WHERE a.id_member = m.id_member 
      AND a.variable = 'cust_armaus' 
      ORDER BY a.value LIMIT 1 
     ) AS cust_armaus 
     , (SELECT u.value 
      FROM smf_themes u 
      WHERE u.id_member = m.id_member 
      AND u.variable = 'cust_usermo' 
      ORDER BY u.value LIMIT 1 
     ) AS cust_usermo 
    FROM smf_members m 
    ORDER BY m.id_member 

如果(id_member,variable)元組是保證在smf_themes獨一無二的,你可以使用一個簡單的外部聯接:

SELECT m.id_member AS id 
     , m.real_name 
     , a.value AS cust_armaus 
     , u.value AS cust_usermo 
    FROM smf_members m 
    LEFT 
    JOIN smf_themes a 
     ON a.member_id = m.member_id AND a.variable = 'cust_aramus' 
    LEFT 
    JOIN smf_themes u 
     ON u.member_id = m.member_id AND u.variable = 'cust_usermo' 
    ORDER BY m.id_member 

和這兩個查詢,你可以使用代碼類似這樣的:

$sql = "SELECT ..."; 
    $rs = $mysqli->query($sql) 
    if (!$rs) { 
    // handle error 
    echo 'query failed: (' . $mysqli->errno . ') ' . $mysqli->error; 
    die; 
    } 
    // loop through rows returned 
    while ($row = $rs->fetch_assoc()) { 
    echo '<member id="' . htmlspecialchars($row['id']) . '"' 
     . ' nick="' . htmlspecialchars($row['real_name']) . '">' 
     . '<cust_aramus>' . htmlspecialchars($row['cust_aramus']) . '</cust_aramus>' 
     . '<cust_usermo>' . htmlspecialchars($row['cust_usermo']) . '</cust_usermo>' ; 
    }