fulltext search method didn't work - php

i tried to build a search engine in my php script using full text feature,but it didn't get any results.
steps i did:
i altered a 2 fileds to full text
ALTER TABLE stack_ask ADD FULLTEXT(q_title,q_body)
then i added some data
INSERT INTO articles (q_title,q_body) VALUES('MySQL Tutorial','DBMS stands for DataBase ...');
then i made a variable to recieve a post data from input
$q_title=$db->real_escape_string($_POST['search_word']);
then the query:
SELECT * FROM stack_ask WHERE MATCH (q_title,q_body) AGAINST ('$q_title');
when i tried to search something like database it didn't give any result and output the following error:
call to amember function fetch_assoc on a non-object
what's the problem?

Hi Mohamed amin,
Make sure that q_title,q_body has indexing or not. Read this article
may it will be useful for you.
http://dev.mysql.com/doc/refman/5.0/en/fulltext-natural-language.html

after multiple trials,i solved the problem by the following:
1-i checked the index type of 2 columns using the following sql:
show index from stack_ask
and they were full text
2-i checked the table type ,which was MYISAM
2-the correct sql query should be like the following:
SELECT *, MATCH(q_title,q_body) AGAINST ('$q_title') FROM stack_ask ORDER BY id DESC
not:
"SELECT id, MATCH(q_title,q_body) AGAINST ('$q_title') FROM stack_ask ORDER BY id DESC

Related

Selecting a serialized value from MySQL with a REGEX

I have a serialized field called details in a MySQL DB that looks like this:
a:4:{s:9:"bootstrap";s:14:"Boostrap_3.3.x";s:7:"layouts";s:10:"Responsive";s:13:"preprocessors";s:0:"";s:8:"browsers";s:117:"Latest_Chrome,Latest_Firefox,Latest_Safari,Internet_Explorer_11,Internet_Explorer_10,Internet_Explorer_9,Latest_Opera";}
I'm trying to retrieve records with a value containing Bootstrap_3.x
I've been able to retrieve the details field data based on the key: bootstrap with the following:
SELECT details as bootstrap_version FROM product_info WHERE details LIKE '%bootstrap%'
This query returns all records that have a field called bootstrap.
What do I need to change in order to select values that contain Bootstrap_3.x?
Any help would be appreciated.
hanks,
-Paul
In MySQL you can use regular expressions in your queries:
SELECT details as bootstrap_version FROM product_info
WHERE details REGEXP 'bootstrap";s:\\d+:"Bootstrap_3\.x'
However, if this information is something that you plan on using for search more often, I would suggest putting it in another column that you can index - regexp in queries are very slow.
Read more in the documentation: https://dev.mysql.com/doc/refman/5.7/en/regexp.html
You should user REGEXP:
SELECT details AS bootstrap_version FROM product_info WHERE details REGEXP 'Boostrap_3(\\.[0-9]+)+'

Mysql query using LIKE operator return no result

I am using php my admin to test my query and when I test a simple query like:
SELECT * FROM song WHERE title LIKE '%anytitle%'
It returns an empty result. With any title it gives me the same thing.
If i try the same thing on my other table users
SELECT * FROM users WHERE email LIKE '%gmail%'
It works and return all record containing gmail.
The table are built the same, utf8_unicode_ci and VARCHAR.
What could be the problem?
edit:
I destroyed my table and I have rebuild it.I have executed the query with just one row in the table and it worked.
I've repopulated the table and now it does not work.
I have 5029 records in the table.
Would it be a setting somewhere?
Re edit:
When I try like
SELECT * FROM song WHERE title LIKE '%d%'
It return every record where there is a d inside the title name. But if I try with more than one letter it does not work, I have no result.
I've updated mysql, I've unistalled it and installed version 5.6 instead and still the same issue.

Need to find the database value from greatest id available in output

By running the following SQL command:
SELECT inl_cbsubs_subscriptions.user_id, inl_cbsubs_payment_items.subscription_id,
inl_cbsubs_payment_items.stop_date
FROM inl_cbsubs_subscriptions INNER JOIN
inl_cbsubs_payment_items ON inl_cbsubs_subscriptions.id=inl_cbsubs_payment_items.subscription_id
WHERE inl_cbsubs_subscriptions.user_id=596;
I get the following output:
As you can see, there are a variety of id values that are not always incremental. I need a way to modify the SQL statement so that the search will filter through the results and only provide a single output from the item which has the greatest id value. So, to show what I would like to see from the above example, here is a screenshot:
I am running the SQL statement in a PHP script, so if I need to implement any dynamic variables that would be available. Thanks you for your time.
You can use a DESC sorting on id and limit result with LIMIT 1.

Text matching in sql query (LIKE)

My Database:
ID place
-----------------------------------------------
1 Bhowali
2 Bangalore
3 Tumkur
My Query:
SELECT *
FROM table
WHERE place LIKE '%bhovali'
Question is: many users search wrong keyword on that time the query result should match and output correct one.. in my above query bhovali is a wrong word the correct word is bhowali. is there any way to get correct result from the query??..
may be this can help SELECT * FROM table WHERE SOUNDEX(name) LIKE SOUNDEX('bhovali') ;
This is a really loaded question... short answer is no. SQL is made for giving you what you want, not what you might want.
You would have to develop some kind of intelligence engine to recognize that there are similar names and search for those as it occurs.
in mysql you can use full text search if the column has the full text index as
select * from table match(name) against ('bhovali');

how to delete particular number of rows in a table using php

if i select these particular rows using
select * from wp_posts where title like 'abc%'
it displays the rows
now i want to delete these rows using php
Can i do this or iam wrong
thanks in advance
It would be more interesting if you inform your code a little more complete. But basically the SQL command is as follows:
delete from wp_posts where title like 'abc%'
If there is sensitive data like activation keys or something, you need to use binary function of mysql to check whether abc is matching with ABC or not.
Use this one:
delete from wp_posts where title like binary('ABC%')

Categories