I have made a search engine which works well, but I want it to disregard symbols in the database entries.
e.g. I search for A*B-C
In the database I have a column that contains ABC.
I would like it to bring back this record even thought it has symbols in.
How would I do this?
Hi, I tried that but it does not work.
Here is my code:
$query = '%' . rawurlencode($queryRaw) . '%' ;
$queryClean = ereg_replace("[^A-Za-z0-9]", "%", $query) ;
$result = $dbh->prepare("SELECT supplier_details.id as supid, name, languages.languages, countries.country
FROM supplier_details, languages, countries
WHERE languageRef = languages.id
AND countryRef = countries.id
AND (name LIKE ? OR name LIKE ?)
LIMIT 50") ;
$result->bindParam(1, $query, PDO::PARAM_INT) ;
$result->bindParam(2, $queryClean, PDO::PARAM_INT) ;
$result->execute() ;
select *
from tbl
where clmn like '%A%B%C%'
Related
I am running problems in implementing LIKE in PDO
I have this query:
$query = "SELECT * FROM tbl WHERE address LIKE '%?%' OR address LIKE '%?%'";
$params = array($var1, $var2);
$stmt = $handle->prepare($query);
$stmt->execute($params);
I checked the $var1 and $var2 they contain both the words I want to search, my PDO is working fine since some of my queries SELECT INSERT they work, it's just that I am not familiar in LIKE here in PDO.
The result is none returned. Do my $query is syntactically correct?
You have to include the % signs in the $params, not in the query:
$query = "SELECT * FROM tbl WHERE address LIKE ? OR address LIKE ?";
$params = array("%$var1%", "%$var2%");
$stmt = $handle->prepare($query);
$stmt->execute($params);
If you'd look at the generated query in your previous code, you'd see something like SELECT * FROM tbl WHERE address LIKE '%"foo"%' OR address LIKE '%"bar"%', because the prepared statement is quoting your values inside of an already quoted string.
Simply use the following:
$query = "SELECT * FROM tbl WHERE address LIKE CONCAT('%', :var1, '%')
OR address LIKE CONCAT('%', :var2, '%')";
$ar_val = array(':var1'=>$var1, ':var2'=>$var2);
if($sqlprep->execute($ar_val)) { ... }
No, you don't need to quote prepare placeholders. Also, include the % marks inside of your variables.
LIKE ?
And in the variable: %string%
$query = "SELECT * FROM tbl WHERE address LIKE ? OR address LIKE ?";
$params = array("%$var1%", "%$var2%");
$stmt = $handle->prepare($query);
$stmt->execute($params);
You can see below example
$title = 'PHP%';
$author = 'Bobi%';
// query
$sql = "SELECT * FROM books WHERE title like ? AND author like ? ";
$q = $conn->prepare($sql);
$q->execute(array($title,$author));
Hope it will work.
First of all I want to excuse me for any english mistakes that might be in my texts, in fact it is not my mother language so it's not really perfect... Anyways:
I am using a table for search tags which contains all search tags and a unique name they belong to. For the actual search query, I use REGEXP (the query is written in PDO style). The user has to type in the search keywords seperated with a comma.
As I executed several tests I noticed that the shown results are depending on the order of the typed in words and the order the search tags are stored in the seach tags table.
My question is: How can I execute a query using REGEXP which does ignore the order of the words?
Till now the actual query is pretty simple:
$search = explode(',', htmlentities($_POST["search"]));
$search = implode('|', $search);
$stmt = $db->prepare("SELECT name FROM blablabla WHERE name REGEXP :search");
$stmt->bindValue(":search", $search, PDO::PARAM_STR);
Thanks!
I am sorry, but I do not know, wether this is possible, at least in a performant way.
I would try to achieve it like that:
$search = explode(',', htmlentities($_POST["search"]));
$sql = 'SELECT name FROM blablabla ';
$query = array();
$parts = array();
foreach($search as $value) {
$query[] = 'name LIKE ?';
$parts[] = '%' . $value . '%';
}
$sql .= ' WHERE ' . implode(' OR ', $query); //Or use logical AND. Just what suits you
$stmt = $pdo->prepare($sql);
$stmt->execute($parts);
Im having a little problem finding a query to do what I want.
Im using Jquerys autocomplete to search for job ID.
Currently the code I'm using is:
$keyword = "%" . (int) $_GET['term'];
$sql = $DB->prepare("SELECT JID, SiteName FROM jobs WHERE CID = :cid AND `JID` LIKE :term ORDER BY JID DESC LIMIT 6");
when the code runs it only returns IDs 1 and 11
I want is so any ID beginning with 1 is displayed eg
1,10,11,12,13,14,15 ... 100 etc
Any ideas how I solve this?
change that
$keyword = "%" . (int) $_GET['term'];
to
$keyword = (int) $_GET['term']. "%" ;
you are cheking numbers which ends by 1 , like that you will check numbers wich starts by 1.
this would be better thought if you using pdo
$keyword = (int) $_GET['term'];
$params = array("$keyword%");
$sql = $DB->prepare(...........);
$sql->execute($params);
After reviewing this SO, I am having some trouble using prepared statements.
I have added the following to my db connection:
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
And here are my query statements:
$query = "SELECT schedules.schedule_name, users.name, schedules.schedule_id
FROM schedules
INNER JOIN users
ON schedules.admin_id=users.user_id
WHERE schedules.schedule_name
LIKE '%:search%'
ORDER BY schedules.schedule_name";
$stmt = $db->prepare($query);
$result = $stmt->execute(array('search' => $search_string));
$search_results = $stmt->fetchAll();
As you can see in the LIKE, I am trying to replace :search when the query is executed but it does not seem to be replaced.
When you do a parameterized query, it is not simply a string replace, and you don't surround it with singlequotes:
$query = "SELECT schedules.schedule_name, users.name, schedules.schedule_id
FROM schedules
INNER JOIN users
ON schedules.admin_id=users.user_id
WHERE schedules.schedule_name
LIKE :search
ORDER BY schedules.schedule_name";
But you want to have % wildcards around the term right? Add them when you inject it:
$result = $stmt->execute(array('search' => '%' . $search_string . '%' ));
I want to create a search query on MySQL database that will consist of 5 different strings typed in from user. I want to query 5 different table columns with these strings.
When I for example have input fields like:
first name, last name, address, post number, city.
How should I query the database that I dont always get all the rows.
My query is something like this:
SELECT user_id, username
from users
where
a like %?% AND
b like %?% AND
c like %?% AND
d like %?% AND
e like %?%;
When I exchange the AND for OR I always get all the results which makes sense, and when I use AND I get only the exact matches...
Is there any function or statement that would help me with this?
EDIT
The code I use is:
$sql = "select users.user_id, first_name
from users
inner join user_normal_aos
on users.user_id = user_normal_aos.user_id
inner join normal_areas_of_expertise
on user_normal_aos.normal_areas_of_expertise_id = normal_areas_of_expertise.normal_areas_of_expertise_id
where
users.first_name like ? AND
users.kanzlei like ? AND
normal_areas_of_expertise.normal_aoe like ? AND
users.postcode like ? AND
users.city like ?";
$query = $this->db->query($sql,
array(
'%'.$lawyer_name.'%',
'%'.$kanzlei.'%',
'%'.$area_of_expertise.'%',
'%'.$post_code.'%',
'%'.$city.'%')
);
For example use PHP to adjust your query based on what fields you have entered.
$where = array();
$replacements = array();
/* you can also compare if string is not null or not empty...
this is just example using isset */
if (isset($lawyer_name)) {
$where[] = 'users.first_name like ?';
$replacements[] = '%'.$lawyer_name.'%';
}
/* repeat this if again for all your fields .... */
$sql = "..... where ".implode(' AND ', $where);
$query = $this->db->query($sql,
$replacements
);