So, I have following php query:
for($i = 0; $i < sizeof($cat_result) ; ++$i) {
$query_p = "SELECT * FROM $table WHERE id = %d";
$results[] = $wpdb->get_row($wpdb->prepare($query_p, $cat_result[$i]));
}
The search relies on $cat_result
which contains numerous numbers.
For example, let say $cat_result
contains the following number: ‘1,3,5,21,35`
Using these 5 numbers, the query will look for the db info.
However, there are scenarios where some number (for example, “21”) do not exist in the db.
Then, I get PHP NOTICE: trying to get property of non-object
.
How do I write “if” statement so that if the id
(in this case “21”) does not exist, then it simply ignores the query? (It is hard to explain what I mean by “ignore”. I still want the query to do the search but if it does not find what it is looking for, then simply ignore the error).
I think you should restructure your query. That way, as long as your data is sanitized (to prevent injection as im not sure if it comes from the user or not), you can just do the following:
That way, it will fetch you all in that list.
Try this:
I don’t know WordPress, but I suspect
get_row
returnsfalse
when there are no more rows of results. Your code was puttingfalse
into$results
when that happened, and later code was then trying to use that as an object.