searching exact word using LIKE - php

Please help...
I need to be able to search an exact words in my database.., I've already used different methods..
Method 1
$param2 = "SELECT * from item WHERE prodname REGEXP '[[:<:]]($param)[[:>:]]'
order by CASE WHEN instr(prodname, '$param') = 0 then 1 else 0 end,
instr(prodname, '$param') ASC";`
This is working really good but when I tried searching words with \ or " it returns an error. I've already used htmlspecialchars and mysql_real_escape_string but the problem still exist..
Method 2
$param2 = "WHERE prodname LIKE '$param %' OR prodname LIKE '% $param'
OR prodname LIKE ' $param%' OR prodname LIKE '%$param '
OR prodname LIKE '% $param %'
order by CASE WHEN instr(prodname, '$param') = 0 then 1 else 0 end, instr(prodname, '$param') ASC";`
This is also working good but when I type the exact product eg "STAMP PAD INK (RED)" it returns the result "NOT FOUND" but it shows when I only type "STAMP PAD INK".. for some reason It works when I add '%$param%' but when I type an exact word ex. "INK" the word "WRINKLED" also shows and I dont want that.
I can't use fulltext..

For method 2 :
To get the exact product, use: LIKE '$param'
The % character matches any number of characters, even zero characters, but there is still the space characters in '% $param %', for instance.
More info: http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html#operator_like

I think you should try using the PHP function addslashes() for $param as in:
$param = addslashes($param);
This will make method 1 work.
mysql_real_escape_string's behavior is based on the default character set and I don't know which one you are using. Since it's not working for you, then addslashes probably will.
BTW, you may want to look further into how to use fulltext indexing because REGEXP has the potential of becoming mindnumbingly slow as the database grows. And method 2 that you show will perform very slowly, always.

Related

MySQL select only using first word of variable

I am using php and mySQL. I have a select query that is not working. My code is:
$bookquery = "SELECT * FROM my_books WHERE book_title = '$book' OR book_title_short = '$book' OR book_title_long = '$book' OR book_id = '$book'";
The code searches several title types and returns the desired reference most of the time, except when the name of the book starts with a numeral. Though rare, some of my book titles are in the form "2 Book". In such cases, the query only looks at the "2", assumes it is a "book_id" and returns the second entry in the database, instead of the entry for "2 Book". Something like "3 Book" returns the third entry and so forth. I am confused why the select is acting this way, but more importantly, I do not know how to fix it.
If you have a column in your table with a numeric data type (INT, maybe), then your search strategy is going to work strangely for values of $book that start with numbers. You have discovered this.
The following expression always returns true in SQL. It's not intuitive, but it's true.
99 = '99 Luftballon'
That's because, when you compare an integer to a string, MySQL implicitly does this:
CAST(stringvalue AS INT)
And, a cast of a string beginning with the text of an integer always returns the value of the integer. For example, the value of
CAST('99 Luftballon' AS INT)
is 99. So you'll get book id 99 if you look for that search term.
It's pointless to try to compare an INT column to a text string that doesn't start with an integer, because CAST('blah blah blah' AS INT) always returns zero. To make your search strategy work better, you should consider omitting OR book_id = '$book' from your search query unless you know that the entirety of $book is a number.
As others mention, my PHP allowed both numerical enties and text entries from the browser. My query was then having a hard time with this, interpreting some of my text entries as numbers by truncating the end. Thus, my "2 Book" was being interpreted as the number "2" and then being queried to find the second book in the database. To fix this I just created a simple if statement in PHP so that my queries only looked for text or numbers. Thus, in my case, my solution was:
if(is_numeric($book)){
$bookquery = "SELECT * FROM books WHERE book_id = '$book'";
}else{
$bookquery = "SELECT * FROM books WHERE book_title = '$book' OR book_title_short = '$book' OR book_title_long = '$book'";
}
This is working great and I am on my way coding happily again. Thanks #OllieJones and others for your questions and ideas which helped me see I needed to approach the problem differently.
Not sure if this is the correct answer for you but it seems like you are searching for only exact values in your select. Have you thought of trying a more generic search for your criteria? Such as...
$bookquery = "SELECT * FROM my_books WHERE book_title LIKE '".$book."' OR book_title_short LIKE '".$book."' OR book_title_long LIKE '".$book."' OR book_id LIKE '".$book."'"
If you are doing some kind of searching you might even want to ensure the characters before the search key are found as well like so....
$bookquery = "SELECT * FROM my_books WHERE book_title LIKE '%".$book."' OR book_title_short LIKE '%".$book."' OR book_title_long LIKE '%".$book."' OR book_id LIKE '%".$book."'"
The % is a special char that looks for allows you to search for the chars you want to search for PLUS any characters before this that aren't in the search criteri... for example $book = "any" with a % before hand in the query like so, '%".$book."'"`` would return bothcompanyand also the wordany` by itself.
If you need to you can add a % to the end also like so, `'%".$book."%'"`` and it would do the same for the beginning and end of the search key

MySQL "LIKE" search doesn't work

I've imported a .txt database in MySQL through "LOAD DATA INFILE", and everything seemed working, the only problem is that if I search a record on the DB with the following query:
"SELECT * FROM hobby WHERE name LIKE 'Beading'"
it returns 0 rows, while if I use
""SELECT * FROM hobby WHERE name LIKE '%Beading%'"
it returns 1 row, even if exist one record with name=Beading. Does anybody know what could it depend on?
When using SQL LIKE:
You should use a wildcard identifier such as (or not):
'%word' - return results that the value ends with the letters: "word".
'word%' - return results that begin with the letters: "word".
%word% - return results that include the letters: "word" anywhere.
there are some more pattern search combination like:
'abc' LIKE 'abc' true
'abc' LIKE 'a%' true
'abc' LIKE 'b' true
'abc' LIKE 'c' false
If you want an exact match you should use:
"SELECT * FROM hobby WHERE name='Beading'"
But LIKE 'Beading' should also work, so it's probably a spaces issue or case sensitivity problem.
You need to take care of collation case (sensitivity) when you want to make sure your results are complete.
Say your table is UTF...CS and you want to make an insensitive case search you should declare it in your SQL query, for example:
SELECT * FROM hobby WHERE name COLLATE UTF8_GENERAL_CI LIKE 'beading'
try testing different approaches and see what fits your goal best.
the first one doesnt work because Like statment you should use '%search word%'
you should use this one
"SELECT * FROM hobby WHERE name LIKE '% Beading %' or name LIKE '% Beading'
or name LIKE 'Beading %' or name LIKE 'Beading'"

Why query is not working?

In model query -
public function get_articles_from_query($squery){
$query = DB::query(Database::SELECT, 'SELECT * FROM ieraksti WHERE virsraksts = "%:squery%" AND slug = "%:squery%" AND saturs = "%:squery%"')
->parameters(array(":squery" => $squery))->execute();
return $query;
}
Why this script is not working? I not have any errors, but blog articles not found.
Well, my first question is:
Do you have any rows that have that word, and nothing else, surrounded by % characters, in all three of the the title, content and slug (a)? This seems unlikely.
If not, the query will return no rows.
The use of the % wildcards is for like, not for =. If your squery word is banāns for example, your query will only match rows where all three columns are exactly the literal %banāns% value, not a value containing banāns somewhere within it.
I think you want:
SELECT * FROM ieraksti
WHERE virsraksts like "%:squery%"
AND slug like "%:squery%"
AND saturs like "%:squery%"
For your subsequent problem:
I understand the problem. I put in model var_dump($query); and it shows the query - SELECT * FROM ieraksti WHERE virsraksts like "%'ar'%" OR slug like "%'ar'%" OR saturs like "%'ar'%. The is not word 'ar', but ar. How to remove ' from query?
If that's the case, you will have to change your query to something like:
$query = DB::query(Database::SELECT,
'SELECT * FROM ieraksti WHERE virsraksts like :squery AND slug ...')
->parameters(array(":squery" => "%$squery%"))->execute();
In other words, move the attachment of the % characters to the parameter specification, not the query itself. From your debug, the parameters are automatically having single quotes wrapped around them so, if you want to do anything inside those quotes, you have to do it before the wrapping.
And you obviously don't need the double quotes in this case because that wrapping is taking place.
And check that line with "%$squery%" in it, I'm not sure it's the right syntax. I'd say my PHP was a bit rusty but, to be honest, I've only ever used it for about seven minutes in my whole life :-)
What you're aiming for is a string consisting of %, the value of $squery and another %.
(a) Man tiešām patīk, kā Google Translate padara mani izskatās es zinu desmitiem dažādās valodās :-)

PHP mysql search queries

I'm trying to create a search engine for an inventory based site. The issue is that I have information inside bbtags (like in [b]test[/b] sentence, the test should be valued at 3, whereas sentence should be valued at 1).
Here is an example of an index:
My test sentence, my my (has a SKU of TST-DFS)
The Database:
|Product| word |relevancy|
| 1 | my | 3 |
| 1 | test | 1 |
| 1 |sentence| 1 |
| 1 | TST-DFS| 10 |
But how would I match TST-DFS if the user typed in TST DFS? I would like that SKU to have a relevancy of say 8, instead of the full 10..
I have heard that the FULL TEXT search feature in MySQL would help, but I can't seem to find a good way to do it. I would like to avoid things like UNIONS, and to keep the query as optimized as possible.
Any help with coming up with a good system for this would be great.
Thanks,
Max
But how would I match TST-DFS if the user typed in TST DFS?
I would like that SKU to have a relevancy of say 8, instead of the full 10..
If I got the question right, the answer is actually easy.
Well, if you forge your query a little before sending it to mysql.
Ok, let's say we have $query and it contains TST-DFS.
Are we gonna focus on word spans?
I suppose we should, as most search engines do, so:
$ok=preg_match_all('#\w+#',$query,$m);
Now if that pattern matched... $m[0] contains the list of words in $query.
This can be fine-tuned to your SKU, but matching against full words in a AND fashion is pretty much what the user presumes is happening. (as it happens over google and yahoo)
Then we need to cook a $expr expression that will be injected into our final query.
if(!$ok) { // the search string is non-alphanumeric
$expr="false";
} else { // the search contains words that are no in $m[0]
$expr='';
foreach($m[0] as $word) {
if($expr)
$expr.=" AND "; // put an AND inbetween "LIKE" subexpressions
$s_word=addslashes($word); // I put a s_ to remind me the variable
// is safe to include in a SQL statement, that's me
$expr.="word LIKE '%$s_word%'";
}
}
Now $expr should look like "words LIKE '%TST%' AND words LIKE '%DFS%'"
With that value, we can build the final query:
$s_expr="($expr)";
$s_query=addslashes($query);
$s_fullquery=
"SELECT (Product,word,if((word LIKE '$s_query'),relevancy,relevancy-2) as relevancy) ".
"FROM some_index ".
"WHERE word LIKE '$s_query' OR $s_expr";
Which shall read, for "TST-DFS":
SELECT (Product,word,if((word LIKE 'TST-DFS'),relevancy,relevancy-2) as relevancy)
FROM some_index
WHERE word LIKE 'TST-DFS' OR (word LIKE '%TST%' AND word LIKE '%DFS%')
As you can see, in the first SELECT line, if the match is partial, mysql will return relevancy-2
In the third one, the WHERE clause, if the full match fails, $s_expr, the partial match query we cooked in advance, is tried instead.
I like to lower case everything and strip out special characters (like in a phone number or credit card I take everything out on both sides that isn't a number)
Rather than try to create your own FTS solution, you could try to fit the MySQL FTS engine to your requirements. What I've seen done is create a new table to store your FTS data. Create a column for each different piece of data that you want to have a different relevance. For your sku field you could store the raw sku, with spaces, underscores, hyphens and any other special character intact. Then store a stripped down version with all these things removed. You may also want to store a version with leading zeros removed, as people often leave things like that out. You can store all these variations in the same column. Store your product name in another column, and the product description in another column. Create a separate index on each column. Then when you do your search, you can search each column individually, and multiply the rank of the results based on how important you think that column is. So you could multiply sku results by 10, title by 5 and leave description results as is. You may have to do a little experimentation to get the results you want, but it may ultimately be simpler than creating your own index.
Create a keywords table. Something along the lines of:
integer keywordId (autoincrement) | varchar keyword | int pointValue
Assign all possible keywords, skus, etc, into this table. Create another table, a post-keywords bridge, (assuming postId is the id you've assigned in your original table) along the lines of:
integer keywordId | integer postId
Once you have this, you can easily add keywords to each post as it is interested. To calculate total point value for a given post, a query such as the following should do the trick:
SELECT sum(pointValue) FROM keywordPostsBridge kpb
JOIN keywords k ON k.keywordId = kpb.keywordId
WHERE kpb.postId = YOUR_INTENDED_POST
I think the solution is quite straightforward unless I missed something.
Basically run two search, one is exact match, the other is like match or regex match.
Join two resultsets together, like match left join exact match. Then for example:
final_relevancy = (IFNULL(like_relevancy, 0) + IFNULL(exact_relevancy, 0) * 3) / 4
I didn't try this myself though. Just an idea.
I would add a column that is stripped of all special character's, misspellings, and then upcased (or create a function that compares on text that has been stripped and upcased). That way your relevancy will be consistent.
/*
q and q1 - you table
this query takes too much resources,
make from it update-query ( scheduled task or call it on_save if you develop new system )
*/
SELECT
CASE
WHEN word NOT REGEXP "^[a-zA-Z]+$"
/*many replace with junk characters
or create custom function
or if you have full db access install his https://launchpad.net/mysql-udf-regexp
*/
THEN REPLACE(REPLACE( word, '-', ' ' ), '#', ' ')
ELSE word
END word ,
CASE
WHEN word NOT REGEXP "^[a-zA-Z]+$"
THEN 8
ELSE relevancy
END relevancy
FROM ( SELECT 'my' word,
3 relevancy
UNION
SELECT 'test' word,
1 relevancy
UNION
SELECT 'sentence' word,
1 relevancy
UNION
SELECT 'TST-DFS' word,
10 relevancy
)
q
UNION
SELECT *
FROM ( SELECT 'my' word,
3 relevancy
UNION
SELECT 'test' word,
1 relevancy
UNION
SELECT 'sentence' word,
1 relevancy
UNION
SELECT 'TST-DFS' word,
10 relevancy
)
q1
it is a page coading where query result shows
**i can not use functions by use them work are more easier**
<html>
<head>
</head>
<body>
<?php
//author S_A_KHAN
//date 10/02/2013
$dbcoonect=mysql_connect("127.0.0.1","root");
if (!$dbcoonect)
{
die ('unable to connect'.mysqli_error());
}
else
{
echo "connection successfully <br>";
}
$data_base=mysql_select_db("connect",$dbcoonect);
if ($data_base==FALSE){
die ('unable to connect'.mysqli_error($dbcoonect));
}
else
{
echo "connection successfully done<br>";
***$SQLString = "select * from user where id= " . $_GET["search"] . "";
$QueryResult=mysql_query($SQLString,$dbcoonect);***
echo "<table width='100%' border='1'>\n";
echo "<tr><th bgcolor=gray>Id</th><th bgcolor=gray>Name</th></tr>\n";
while (($Row = mysql_fetch_row($QueryResult)) !== FALSE) {
echo "<tr><td bgcolor=tan>{$Row[0]}</td>";
echo "<td bgcolor=tan>{$Row[1]}</td></tr>";
}
}
?>
</body>
</html>

MySQL a real LIKE statement

I'm helping a friend build a dictionary-of-sorts for a project he's working on. Part of the project is to create a Search functionality. The database is in MySQL, backend in php.
Now, running our simple query was a piece of cake:
SELECT *,
(
(CASE WHEN word LIKE '%$query%' THEN 1 ELSE 0 END) +
(CASE WHEN defin LIKE '%$query%' THEN 1 ELSE 0 END)
) AS relev
FROM dictionary
WHERE word LIKE '%$q%'
OR defin LIKE '%$q%'
ORDER BY relev DESC;
It produced good results; for example, inputting "fire" gave us fire, firemen, firetruck, on fire, etc. However, we also want room for error: We want the mistake "prnk" to give us prank, prink and also pink, or the word "mule" to also suggest the word "mole".
Quite surprisingly, we weren't able to find any information on it. The relevence system is entirely superficial because we don't need actual relevence (just an overall pointer), but we do need something (and that's why we went for the LIKE statement and not the MATCH...AGAINST statement, where we found nowhere to sort by relevence.)
The database only consists of three things: id, word, defin. Simple as that, as that was the required complexity (or simplicity.)
Thanks to anyone in advance.
Try testing if the word sounds like one in the dictionary, so something along the lines of:
SELECT *,
(
(CASE WHEN word LIKE '%$query%' THEN 1 ELSE 0 END) +
(CASE WHEN defin LIKE '%$query%' THEN 1 ELSE 0 END) +
(CASE WHEN LEFT(SOUNDEX(word), 4) = LEFT(SOUNDEX('$query'), 4) THEN 1 ELSE 0 END) +
(CASE WHEN LEFT(SOUNDEX(defin), 4) = LEFT(SOUNDEX('$query'), 4) THEN 1 ELSE 0 END)
) AS relev
FROM dictionary
WHERE word LIKE '%$q%'
OR defin LIKE '%$q%'
ORDER BY relev DESC;
Regarding the prank...
http://webarto.com/80/did-you-mean-api
$q = "prnk"
$dym = new DYM;
$spell = $dym->check($q);
if(!empty($spell)){
echo $spell; // prank
}
(not really API, not really reliable, but it's working in less than 0.5s)
For mule/mole part try finding Levenshtein implementation for SQL...
http://www.artfulsoftware.com/infotree/queries.php?&bw=1280#552 (link not working but Google it)
http://php.net/manual/en/function.levenshtein.php
Try SOUNDS LIKE:
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#operator_sounds-like
Take a look at this: Soundex (Sounds Like) function in mysql
Apart from Soundex and Levenshtein, you could also look into Metaphone or Double Metaphone, even though the latter ones doesn't have built-in support in MySQL. PHP supports it though, see metaphone - and there are a few double metaphone implementations floating around as well (ie. http://swoodbridge.com/DoubleMetaPhone/).

Categories