query MySQL database with variables from an array
I have my database field named tag_info
in it I will have data that appears like so:
arm leg head forearm foot
Tags are separated by spaces. (not sure if that is the best way so I am open to suggestions)
Now I need to write a query that will return all the rows that have specific tags in the tag_info field. But the tags开发者_如何学编程 I am looking for do not all need to be present just at least one of them does.
$find_tags = array("arm,"leg");
$query = "SELECT * FROM mydatabase WHERE tag_info = '$find_tags'
Is this possible?
You should probably change your database schema around first. Instead of storing tags separated by spaces.
Could you show me the schema (column names) for your table. I think this is a case where you should be having two tables. A separate table just for tags.
That's not a good way to do tagging. You should have a tags table (id, name), yourdatabase_tags (yourdatabaserecord_id, tag_id) so you can associate a record in yourdatabase table to a tag in tags table.
So when you have a list of tags, do a query to get their ids, then query yourdatabase_tags to get the record_ids that have that tag_ids, then query yourdatabase for the records. Or you can combine them into one query (That's an exercise left for the readers :)) )
Mysql syntax does not work that way
here what will work.
$query = "SELECT * FROM mydatabase
WHERE tag_info = '".$find_tags[0]."' OR tag_info = '".$find_tags[1]."'";
In your case since the tags are all in one field you can use a wild card: %
$query = "SELECT * FROM mydatabase
WHERE tag_info LIKE '%".$find_tags[0]."%' OR tag_info LIKE '%".$find_tags[1]."%'";
Query with where tag_info like '%arm%' or tag_info like '%leg%'
but this will be quite inefficient, better to store tags seperately one tag per row, have an index on it.
It's not possible to write a query like that because you will have to dynamically change the amount of OR statements in WHERE blah OR blah. One way to do this would be to grab all the data from the database into a PHP Array. Once in the array you can programatically search through the results and eliminate the ones that do not match.
That is if you want to search for different tags, if you only need a specific query for $find_tags = array("arm,"leg"); the other answers will work.
you can try below query.
$query = "SELECT * FROM mydatabase WHERE tag_info IN ('$find_tags');
if you are using a table name and field name correct then this may be needful to you.
Thanks.
$keywords = array("arm", "leg");
$length = count($keywords);
$searchCriteria = '';
for($i = 0; $i < $length; $i++)
{
$searchCriteria = $searchCriteria . "tag_info like '%" . $keywords[$i] . "%'";
if( ($i+1) < $length)
$searchCriteria = $searchCriteria . " OR ";
}
$query = "SELECT * FROM mydatabase
WHERE $searchCriteria";
You may need to add some additional checking to ensure that you don't run into error if the array containing the tags is empty.
The more correct approach would be to have a tag table with the availeble tags and a link table between tag_info and tag_table.
But here is a solution with a query
$query = "SELECT * FROM mydatabase WHERE ";
foreach($find_tags as $tag)
{
$query .= "tag_field LIKE %$tag% OR ";
}
$query = substr($query, 0, strlen($query) - 3);
Not tested!!
Sure, use a LIKE in the WHERE clause instead of =.
SELECT * FROM mydatabase WHERE tag_info LIKE '% foot %' or tag_info LIKE '% leg %'
your query should look something like
SELECT * FROM mydatabase WHERE tag_info = 'arm' OR tag_info = 'leg'
You could also use the MySQL IN
clause:
SELECT * FROM mydatabase WHERE tag_info IN ('arm', 'leg')
This way is easier to build the query string
Edit
I just saw that you are holding the tags serialized in the DB. Now, that is definitely not good because you are destroying the concept of the structured data.
You should create a table called tags
and hold each tag on its own row. Than create another pivot table between the table you wish to tag (the one that holds the serialized tags now) and the tags table.
The pivot table can hold just 2 fields - main_table_id
and tag_id
- this way, you will be able to query your DB with a single join like:
SELECT t.* FROM tags t
INNER JOIN pivot_table pt ON pt.tag_id = t.id
INNER JOIN main_table mt ON mt.id = pt.main_table_id
WHERE tag IN ('arm', 'leg')
精彩评论