0

i am developing an small application which disease after asking about symptoms,,php + mysql

my table is enter image description here

i have an array of symptoms, i want to get disease that match to array symptoms

$a= array('fever','pain');
$sql=mysql_query("select * from disease where `d_symptoms` like '$a'");

already tryed using join and in

echo $v=join(',',$a);
$sql=mysql_query("select * from disease where `d_id` in ($v)");

please help me

Hardik Solanki
  • 3,153
  • 1
  • 17
  • 28
NEET JASSI
  • 85
  • 1
  • 3
  • 12

4 Answers4

1

you need to have a new table called symptoms, which includes a foreign key of the disease id (d_id) in your current table and the symptom name (d_symptom). Then each row will have the name of the symptom and the id of the disease it is linked with. That way you will not have multiple values in the symptom field. You then call it be selecting all symptoms where id='d_id' to get the list of symptoms associated with that disease.

the query might be

$a= array('fever','pain');
$sql=mysql_query("SELECT d_name FROM disease, symptoms WHERE disease.d_id = symptoms.d_id AND d_symptom IN ($a)";);

or something..

gavgrif
  • 15,194
  • 2
  • 25
  • 27
1

The correct answer is to properly normalize your database. You shouldn't use comma separated values (as suggested in comments). I am sure you can find many articles teaching normalization, so I won't go into details here.
You need to separate the symptoms from the diseases.

Diseases table

id     | d_name
---------------------
1      | Dengu
2      | Typhoid
3      | Cervical

Symtoms table

id     | s_name
---------------------
1      | Fever
2      | Pain
3      | Vomit
4      | Abc
5      | Xyz

Diseases-Symptom table (this is an intersection)

id     | d_id    | s_id
---------------------------
1      | 1       | 1
2      | 1       | 2
3      | 1       | 3
2      | 2       | 3
3      | 2       | 2
1      | 2       | 4
2      | 3       | 2
3      | 1       | 5

This way you don't create duplicate symptoms and makes your data easier to use and present, for example

SELECT id, s_name FROM symptoms

will give you a list of all symptoms available.

SELECT diseases.id, diseases.d_name, symptoms.s_name 
FROM diseases 
JOIN diseases_symptoms ON d_id = diseases.id
JOIN symptoms ON symptoms.id = diseases_symptoms.s_id
WHERE diseases.id = 1;

will give you a result similar to:

id     | d_name    | s_name
---------------------------
1      | Dengu     | Fever
2      | Dengu     | Pain
3      | Dengu     | Vomit
Community
  • 1
  • 1
IROEGBU
  • 948
  • 16
  • 33
-1

You may use a single FIND_IN_SET for each symtoms you are looking for:

    $query = "SELECT * FROM disease WHERE 1=1 ";
    foreach($a as $row)
        $query += "AND FIND_IN_SET($row, d_symptoms)";
   $sql=mysql_query($query);
codeGig
  • 1,024
  • 1
  • 8
  • 11
-2

Well, you shouldn't store multiple values in a single column, as a best practice rule.(I really would fix that).

But, maybe something like this would work if you want to continue the way you have it:

$query = "select * from disease where d_symptoms like " . $a[0];
for($i = 1; $i < count($a); i++){
    $query = $query + " AND d_symptoms like " $a[$i];
}

$sql=mysql_query($query);
Ryan G
  • 380
  • 1
  • 11