How to connect table1 to table2 from database


Im making base search in our office to search patient’s profile. I’m using the system that given to us from main office.

table1 = patient profile
code, fname, mname, lname, sname,

table2 = patient diagnose
code, ddate, adate, pdiagnose

those two table only code that connect them,


    $valueToSearch = $_POST['valueToSearch'];
    $query = "SELECT * FROM `table1` WHERE CONCAT(`code`, `fname`, `mname`, `lname`, `sname`) LIKE '%".$valueToSearch."%'";
	$search_result = filterTable($query);
 else {
    $query = "SELECT * FROM `table1`";

    $search_result = filterTable($query);

// function to connect and execute the query
function filterTable($query)
    $connect = mysqli_connect("", "root", "", "h_dbo");
    $filter_Result = mysqli_query($connect, $query);
    return $filter_Result;


this is my code,
Example if i Search Patient David

1 David Coby Lico
1 David Coby Lico Urinary Tract Infection

First of all: never ever use a Post variable directly in a database query. If you use this code in your office, I can delete your whole database by entering a search text. Please use prepared statements.

Second: never ever use Select *. Imagine you will extend the table some times and add some columns containing images. Then you will always load a huge amount of data which is never used.
Always precise which columns you need in the query.

Your query can be done by JOIN

SELECT pa.code, fname, mname, lname, sname, ddata, adata, pdisagnose
FROM table 1 pa
LEFT JOIN table 2 di on di.code = pa.code

Third: your LIKE statement will not work this way. I don’t know in which of the name columns you expect to find the search term. Maybe in all?

This look like this

The patient code is #, only patient code that connects to another table of the database, i want to put the patient diagnose,

Have you solved this? If not, post the code that you have got so far and we can see if we can help.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.