Yes, it is absolutely ok to return error in case of empty search phrase.
Returning empty set does not make much sense to me. If it were to be a success response, I'd rather expect first "default limit" items to be returned, that all match the condition of containing an empty string - which is basically equivalent to no condition at all.
As for your code, the q parameter can come also as an array. You should check not just that it is not empty, but also that it is a string.
For the response, if that's supposed to be aformatted as JSON, then use proper a JSON serialization method
json_encode(['code' => 400, 'message' => '...']);
Your response
{code:412,message:"parameter q is empty"}
is actually not a valid JSON, though it may be a valid javascript fragment. But in JSON, property names (code,message) must be also quoted.
I changed the code to 400, as 412 is really not much appropriate. And this should be returned as the HTTP status code rather then part of the response body. But these were already pointed out by Sam.