I'm just new here.
I would like to ask if my following SQL is correct and where did I do wrong?
This is my SQL in my php file
$sql = "SELECT * FROM tblAlumni WHERE fname LIKE '%$search_file%' or mname LIKE '%$search_file%' or lname LIKE '%$search_file%' AND alum_status LIKE 2"
It got no errors whatsoever but it doesn't display the correct data.
This is my table
Click here
and the result if i've search it or query it is this
Click here Thank you for future answers.
Your query will be executed like this
SELECT *
FROM tblAlumni
WHERE fname LIKE '%$search_file%'
OR mname LIKE '%$search_file%'
OR ( lname LIKE '%$search_file%'
AND alum_status LIKE 2 )
because AND has higher precedence than OR. Parenthesis are very important for execution of Where clause
SELECT *
FROM tblAlumni
WHERE ( fname LIKE '%$search_file%'
OR mname LIKE '%$search_file%'
OR lname LIKE '%$search_file%' )
AND alum_status = 2
Use = instead of LIKE when you are looking for exact match it makes more sense to me
Update :
SELECT *
FROM tblAlumni
WHERE ( fname LIKE '%$search_file%'
OR mname LIKE '%$search_file%'
OR lname LIKE '%$search_file%' )
AND alum_status LIKE 2
AND yeargrad LIKE '$year'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With