它显示几种疾病。我只想根据我输入的内容在数据库中检索最近的疾病。然后,结果是狼疮和麻疹

<?php

    include('connect.php');

        //Function to sanitize values received from the form. Prevents SQL injection

        //Sanitize the POST values


            $symptom1 = $_POST['symptom1'];
            $symptom2 = $_POST['symptom2'];
            $symptom3 = $_POST['symptom3'];
            $symptom4 = $_POST['symptom4'];

            $sql = mysql_query("SELECT disease FROM diseases WHERE
            symptom1 LIKE '%". $symptom1."%' OR symptom1 LIKE '%". $symptom2."%' OR symptom1 LIKE '%". $symptom3."%' OR symptom1 LIKE '%". $symptom4."%' AND
            symptom2 LIKE '%". $symptom1."%' OR symptom2 LIKE '%". $symptom2."%' OR symptom2 LIKE '%". $symptom3."%' OR symptom2 LIKE '%". $symptom4."%' AND
            symptom3 LIKE '%". $symptom1."%' OR symptom3 LIKE '%". $symptom2."%' OR symptom3 LIKE '%". $symptom3."%' OR symptom3 LIKE '%". $symptom4."%' AND
            symptom4 LIKE '%". $symptom1."%' OR symptom4 LIKE '%". $symptom2."%' OR symptom4 LIKE '%". $symptom3."%' OR symptom4 LIKE '%". $symptom4."%'");

            while($row = mysql_fetch_array($sql))
            {
                    echo $row['disease'];
            }
            //end of while loop

?>


query code

error

最佳答案

$query="
    SELECT
      disease,
      (
        symptom1 LIKE '%" . $symptom1 . "%'
        OR symptom1 LIKE '%" . $symptom2 . "%'
        OR symptom1 LIKE '%" . $symptom3 . "%'
        OR symptom1 LIKE '%" . $symptom4 . "%'
      ) + (
        symptom2 LIKE '%" . $symptom1 . "%'
        OR symptom2 LIKE '%" . $symptom2 . "%'
        OR symptom2 LIKE '%" . $symptom3 . "%'
        OR symptom2 LIKE '%" . $symptom4 . "%'
      ) + (
        symptom3 LIKE '%" . $symptom1 . "%'
        OR symptom3 LIKE '%" . $symptom2 . "%'
        OR symptom3 LIKE '%" . $symptom3 . "%'
        OR symptom3 LIKE '%" . $symptom4 . "%'
      ) + (
        symptom4 LIKE '%" . $symptom1 . "%'
        OR symptom4 LIKE '%" . $symptom2 . "%'
        OR symptom4 LIKE '%" . $symptom3 . "%'
        OR symptom4 LIKE '%" . $symptom4 . "%'
      ) AS `score`
    FROM
      diseases
    HAVING `score` > 0
    ORDER BY `score` DESC";

08-06 19:09