I am trying to get data from two different tables by entering a student ID in a search form and then getting the ID, Name from the first table and matching it with the Address in the second table.
If I use the search results from a form the query doesn’t return anything.
$sql = "SELECT Students.StudentID, Students.StudentName, HomeADD.StudentADD
FROM Students
INNER JOIN HomeADD
WHERE Students.StudentID = '%$searchq%'
AND HomeADD.StudentID = '%$searchq%'";
$result = mysqli_query ($conn,$sql);
$count = mysqli_num_rows ($result);
$none = "";
To test the query I replaced $searchq with a literal J34765 and now it works.
$sql = "SELECT Students.StudentID, Students.StudentName, HomeADD.StudentADD
FROM Students
INNER JOIN HomeADD
WHERE Students.StudentID = 'J34765'
AND HomeADD.StudentID = 'J34765";
$result = mysqli_query ($conn,$sql);
$count = mysqli_num_rows ($result);
$none = "";
To further test I used echo to output $searchq to make sure it was captured correctly and it was. I can’t understand why the query using the search results doesn’t work. The expected results are below.
Student ID Student Name Student Address
---------- --------------- -----------------------------------
J34765 John Doe 123 Main Street, Fresno, California