php
  • mysql
  • survey
  • 2011-07-15 52 views 0 likes 
    0

    目前我使用此代碼統計調查的答覆從MySQL數據庫來算調查答覆:一個更合適的方法

    $VERYHAPPY = mysql_query("SELECT * FROM answers, complete WHERE (answers.uniqueID = complete.uniqueID) AND (complete.timestamp) IS NOT NULL AND (answers.surveyID='$surveyID') AND QID = '$QID' AND response = 'VERY HAPPY' "); 
    $HAPPY = mysql_query("SELECT * FROM answers, complete WHERE (answers.uniqueID = complete.uniqueID) AND (complete.timestamp) IS NOT NULL AND (answers.surveyID='$surveyID') AND QID = '$QID' AND response = 'HAPPY' "); 
    $DONTKNOW = mysql_query("SELECT * FROM answers, complete WHERE (answers.uniqueID = complete.uniqueID) AND (complete.timestamp) IS NOT NULL AND (answers.surveyID='$surveyID') AND QID = '$QID' AND response = 'DONT KNOW' "); 
    $UNHAPPY = mysql_query("SELECT * FROM answers, complete WHERE (answers.uniqueID = complete.uniqueID) AND (complete.timestamp) IS NOT NULL AND (answers.surveyID='$surveyID') AND QID = '$QID' AND response = 'UNHAPPY' "); 
    $VERYUNHAPPY = mysql_query("SELECT * FROM answers, complete WHERE (answers.uniqueID = complete.uniqueID) AND (complete.timestamp) IS NOT NULL AND (answers.surveyID='$surveyID') AND QID = '$QID' AND response = 'VERY UNHAPPY' "); 
    
    $VERYHAPPYcount = mysql_num_rows($VERYHAPPY); 
    $HAPPYcount = mysql_num_rows($HAPPY); 
    $DONTKNOWcount = mysql_num_rows($DONTKNOW); 
    $UNHAPPYcount = mysql_num_rows($UNHAPPY); 
    $VERYUNHAPPYcount = mysql_num_rows($VERYUNHAPPY); 
        echo "VERY HAPPY = $VERYHAPPYcount</br>HAPPY = $HAPPYcount</br>DON'T KNOW = $DONTKNOWcount</br>UNHAPPY = $UNHAPPYcount</br>VERY UNHAPPY = $VERYUNHAPPYcount"; 
    

    ,你可以看到 - 這是真的亂了!任何更好的方式來做到這一點?我試過COUNT和GROUP BY,但無法讓他們工作。由於

    +0

    作爲一個方面說明,我d建議查看PHP中的PDO功能。一個小的學習曲線(與現在的曲線大致相同)和靈活性大大增加。 http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/ – Ben

    +0

    *你回來了什麼信息?你是否在你的結果對象上嘗試了'var_dump'或'printr'來查看你回來的東西? – Ben

    回答

    0

    你可以試試這個SQL(我沒有測試) -

    SELECT count(*) as recordcount, response 
    FROM answers, complete 
    WHERE (answers.uniqueID = complete.uniqueID) 
    AND (complete.timestamp) IS NOT NULL 
    AND (answers.surveyID='$surveyID') 
    AND QID = '$QID' 
    GROUP BY response 
    

    要得到的結果爲PHP,你可以嘗試這樣的事情(我不能測試PHP代碼,所以我基於下面的代碼在這個代碼 - !http://php.net/manual/en/function.mysql-query.php和最好的希望下面的代碼幾乎可以肯定將包含錯誤,但將有希望給你如何可以做到的想法)

    //assign sql above to $query variable prior to doing this 
    $result = mysql_query($query); 
    
    while ($row = mysql_fetch_assoc($result)) { 
        echo $row['response'] & " = " & $ row['recordcount'] & "count</br>" 
    } 
    
    +0

    感謝此 - 但我不確定如何打印/迴應結果? –

    +0

    我已經編輯了答案,並解釋瞭如何才能做到這一點 – ipr101

    相關問題