php - Search Bar TypeError
Good night everyone!
TypeError Argument 2 passed to CodeIgniter\Database\BaseBuilder::like() must be of the type string, null given, called in D:\xampp\htdocs\lordeCI\app\Controllers\AutocompleteSearch.php on line 31
Codeigniter is throwing this error for the chunk of code below, 'titulo' and 'post_id' are column names of a table called 'posts':
public function ajaxSearch()
{
helper(['form', 'url']);
$data = [];
$db = \Config\Database::connect();
$builder = $db->table('posts');
$query = $builder->like('titulo', $this->request->getVar('q')) //this is line 31
->select('post_id, titulo as text')
->limit(10)->get();
$data = $query->getResult();
echo json_encode($data);
}
Also, some images aren't loading properly, the browser gives a status code: 200 though content-type is "text/html", i've checked the image path and it is correct. However the message below showed up in dev tools, network tab.
"DevTools failed to load SourceMap: Could not load content for chrome-extension://pmncamalnkoafdfdmojmhanhngjaakcb/browser-polyfill.js.map: HTTP error: status code 404, net::ERR_UNKNOWN_URL_SCHEME"
Answer
Solution:
For the exception
You are getting: You are passing empty value to the q
param, the exception happens because it is like calling: $query = $builder->like('titulo', null)
, this cannot happen, only strings are accepted as the second parameter for the like
method.
To solve this, validate your parameter before using it:
$titulo = '';
if ( ! empty( $titulo ) ) {
$titulo = $this->request->getVar('q');
}
$query = $builder->like('titulo', $titulo);
A better way to do this is through Form Validation.
The images problem
You must check how you are returning your images to your client. If you return a image directly from an echo
you will have this kind of error.
Best way is to solve this in your app server level.
The SourceMap error
Not that big of a problem, this will not hinder your application in anything. Take a look at this StackOverflow's question.
Source