John Davidson

php - LIKE ILIKE SQL operators doesn't work correclty

0 comments
Message:


I have database with phone numbers.


Like this number 00987456321 I want to be able to search on the number by typing the whole number or part of it for example 321 or 4563 or 7456321


From what I know I need "Like" or "Ilike" operators


I tried both but it gave me all numbers in the database.


I tried with "=" and it gives me the phone number but only if I write it completely so my code is working but the operators doesn't work.


This is my code:


<?php
$searchFields['Leads'] = array (
'phone' =>
array (
'query_type' => 'default',
'operator' => 'ILIKE',
'db_field' =>
array (
0 => 'phone_mobile',
1 => 'phone_work',
2 => 'phone_other',
3 => 'phone_fax',
4 => 'phone_home',
),
),

What I'm doing wrong?



My Answer:
It looks like your code is correct in terms of setting up the search fields and using the ILIKE operator. However, the issue might be with the way you are constructing your query.

Make sure that you are constructing your query properly with the ILIKE operator and the search term. Here is an example of how you can construct your query:

php
$searchTerm = '321'; // This is the search term you want to use

$query = "SELECT * FROM Leads WHERE ";
foreach($searchFields['Leads']['phone']['db_field'] as $field) {
$query .= $field . " ILIKE '%" . $searchTerm . "%' OR ";
}
$query = rtrim($query, " OR "); // Remove the last 'OR' from the query

// Now you can execute the query and fetch the results


This code will construct a query that searches for the search term in all the phone number fields using the ILIKE operator. Make sure to adjust the search term variable and the query construction based on your specific requirements.

Rate this post

3 of 5 based on 2833 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us