MySQL中的疾病 - 症状关系

时间:2015-05-29 10:26:50

标签: php mysql sql search

我正在建立一个与患者诊断PHP和MySQL相关的电子健康系统。我在数据库中使用以下记录制作了以下表格:

Illness(illness_id(PK), illness_code,illness_name)
Symptom(symptom_id, illness_id(FK),symptom_name ).

现在,我想做的是,我会在搜索栏中写下症状名称,点击按钮后,应输出相关疾病。你能告诉我SQL查询会输出适当的疾病吗?

2 个答案:

答案 0 :(得分:0)

请试试这个。将症状值从搜索栏检索到变量后,例如symptom_name_provided_in_search_bar,您可以在以下查询中使用该值

select illness_name
from   illness a, symptom b
where  a.illness_id = b.illness_id
and    b.symptom_name = :symptom_name_provided_in_search_bar

答案 1 :(得分:0)

为此,您应该只创建一个包含2行的表疾病: 疾病名称和疾病症状。注意:仅当症状写入数据库中时才会生效。

<?php
$host = 'yourmysqlhost';
$user = 'yourmysqluser';
$pass = 'yourmysqlpassword';
$db = 'yourmysqldatabase';
$symptom = $_POST['symptom'];
$connect = mysqli_connect($host, $user, $pass, $db);
$sanitizedSymptom = mysqli_real_escape_string($connect, $symptom);
$query = mysqli_query($connect, "SELECT * FROM illness WHERE illnessSymptom = '".$sanitizedSymptom."'");
if(mysqli_num_rows($query) == 0)
{
echo '<p>No results...</p>';
}
else
{
while($row = mysqli_fetch_assoc($query))
{
echo '<h1>'.$row['illnessName'].'</h1>';
echo '<p>Symptom: '.$row['illnessSymptom'].'</p>';
echo '<br>';
}
}
?>

编辑:

要查找与数据库中的症状大致类似的症状,查询应如下所示:

$query = mysqli_query($connect, "SELECT * FROM illness WHERE illnessSymptom LIKE '%".$sanitizedSymptom."%'");