Selecting a serialized value from MySQL with a REGEX - php

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]+)+'

Related

MySql select values of serialized column with PHP [duplicate]

How do i search from serialize field in mysql database except mysql like statement?
The data is:
a:9:{s:2:"m1";s:4:"1217";s:2:"m2";s:8:"9986-961";s:2:"m3";s:19:"1988-03-07 00:00:00";s:2:"m4";s:0:"";s:2:"m5";s:0:"";s:2:"m6";s:0:"";s:2:"m7";s:3:"104";s:2:"m8";s:6:"150000";s :2:"m9";s:18:"Ok Then, Yes It Is";}
I need the row in which the m9 value is 'Yes It Is'. I do not want to use mysql 'like' statement.
I have tried:
SELECT * FROM table WHERE field like '%Yes It Is%'
Can you please help.
Have you tried the following:
SELECT * FROM table WHERE field like '%"m9";s:18:"Ok Then, Yes It Is";%'
?
But in fact if you want to search in such data, you should simple create proper structure of your table and not to put all serialized data in one column

How to match Value from string in Mysql PHP

I have a table methodology in mysql where a column contain values like 1,2,3,4,5,6 or 3,4,6 or 25,4,7,8 in multiple row.
Now in my scenario i have a company which contain id 6 and i want to Match this value with methodology table value. But i don't have any idea how i can do it.
I am trying that first it get all values from methodology table one by one and after that match value of company.
can anyone please help me??
You can use FIND_IN_SET(your_id, columnname) function.
Query will be like
SELECT * FROM
methodology
where FIND_IN_SET(your_id, columnname)
For more details about function please refer : http://www.w3resource.com/mysql/string-functions/mysql-find_in_set-function.php
Use LIKE from mysql queries.
An example :
SELECT row FROM my_table WHERE my_column LIKE %6%
You need to use find_in_set function
http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_find-in-set
To elaborate on #matthiasbe's incomplete answer, if you want to use LIKE, you should do as follows :
SELECT * FROM methodology WHERE companies LIKE '%,6,%' OR companies LIKE '6,%' OR companies LIKE '%,6'
This will match all rows where companies either has ,6, in its value or begins with 6, or ends with ,6.
This is far from being a perfect solution (both in clarity of the code and execution time), I'm just listing the options, hope it helps.

fulltext search method didn't work

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

Search entire table for a keyword

I have a trivial question. Im using PHP+MySQL managing a huge DB
I want to search in a entire table a keyword I write in a input.
The problem is that the main table have +100 columns, so I had to write the php query manually
[...]
$sql="select *
from db
where ID LIKE '%".$q."%' or USER_ID LIKE '%".$q."%' or Phone_ID LIKE '%".$q."%' or
Fax_ID LIKE '%".$q."%' or email_ID LIKE '%".$q."%' or [...]
And this is a chaos when I modify a column, or add/remove...
Exist any other way to make this search? If not, I tought about create a separate PHP function, that obtains all column header names, and create an auto-fill function inside.
I tried to look for info with no success
https://stackoverflow.com/search?q=search+entire+table
Unfortunately there isnt any simple way to do this.
One option is to select all columns in table, fetch them as array and iterate over them and build your WHERE clause.
select column_name from information_schema.columns
where table_name = 'TableName'
This will make whole script slower, if you want to go this way i would recommend you to use some caching.
You could get the column info for the 'main table' using info from the information schema. Here are some methods for using MySQL. Here is how to do it using PHP.
You can do a SHOW COLUMNS on the table, then loop over the Field to get all the column names in the table, at least that way you don't have a hand-coded mess to deal with.

how can i find best match for a table in mysql db

I have a table in mysql database(product{id, name, category}) which consists of hundreds of records.
Now a new product is added with the same fields and i want to search for the fields and match it with others records so that i could found the best match of product.
My question is what is the best approach for this, should i search through the name of product but what if there are more than one match with that names.
should i use something like this
SELECT * FROM table_name WHERE column1 LIKE '%column1%' OR column2 LIKE '%column2%'
You are looking for the MySQL "MATCH()" function. This comes with a couple caveats:
This requires a fulltext index (meaning you must be using the MyISAM Engine for that table)
Requires MySQL >= 5.1
From the Manual:
When MATCH() is used in a WHERE clause, as in the example shown earlier, the rows returned are automatically sorted with the highest relevance first.
http://dev.mysql.com/doc/refman/5.5/en/fulltext-natural-language.html
You should look inty MySQL full-text indexing capabilities for generating relevance-sorted output. There are a number of posts on that subject. See for instance this post.

Categories