(PHP) Checking if a field is empty in a database query call and output data if it isn't
I'm putting together a database query for a search engine where you can specify whether the row should have an imagestring or not in the result. In the database this field will be empty if there is开发者_JS百科 no image, or it will have a random image name (e.g. 1238791.jpg). When I construct the query string I check every field and add onto the string what is requested in the search.
For example, if the "Username" field is filled out, it will add "AND username LIKE '%$searchUsername%'"
to the string as an example. However, I'm not sure how to proceed to find out if there is an image (Checking for NULL won't work here) since if there is an image I need to extract the name of the image so I can use it or print it out in the results.
Any help appreciated.
Will this work? Not sure if I fully understand..
select * from tbl where imageName != '' and imageName != null
Edit:
If I pick NO, I will only get results that do not have images (blank/null field). If I pick YES the query will
You will have to check user input and have two queries setup based on the input. Then you should know what fields to pull.
here is a way to do it without adding php-if statements to your sql
select (imageName is not null and imageName <> '') as hasImage, other columns from tbl
精彩评论