Ok. I'm using MySQL and PHP. I have a table called "Pictures" and a field in that table called "Tagged." This field is an array of UserIds which I imploded and stored as a string.
example: 114,159,14,334
Lets say I want to return all the rows in "Pictures" where a particular UserID, say 14, exists somewhere in the "Tagged" field's array. How can this be done?
select * from pictures where FIND_IN_SET(14,Tagged);
Start it with a , and end with a , like this: ,114,159,14,334, and then do a SELECT FROM Pictures WHERE tagged='%,14,%'
Just an idea, I really don't know... I've never done this before. Traditional blog tagging may be similar enough that you could look up some tutorials to learn from them.
Related
I have a table with some submissions, this table has a tags field, and I need to search in it.
The data is saved in JSON format in the table, like this: ["basic","example","html","chart"]
I'm trying to find a way to search all rows in the tags fields, but not sure how it can be done the best way when it is in this format.
The user submits an tag to search, like: html, then I need to search all rows for that tag, without to much overhead.
I know most people use to say: what have you tried yourself?
- well, nothing. As I have no clue how to do this, I know how to search in sql and all that. but never tried it in this logic.
There is no "best way" to search in this format. There is no way at all.
No wonder you have no clue how to do that. I'll tell you more - no one knows it either. Tags should never be stored in json format. It is like as if you built a car, placing wheels on the roof. And then come asking, how to drive it.
You have to learn database basics first. And then create your tables proper way. making a separate table for tags. Storing each on a separate row. After that you will be able to search a tag usual way, using JOIN query to attach the corresponding records to the result.
$sql = "SELECT a.* FROM articles a, tags t WHERE aid=a.id AND tag=?";
$stmt = $pdo->prepare($sql);
$stmt->execute(array($tag));
$data = $stmt->fetchAll();
You should create another table tag with fields name, post_id.
I believe that is the best solution to do a search feature.
If you do not have permission to create database table. It depends on how many posts you have. a few? hundreds or even more? If there is not a huge rows of your post table. You can fetch all of them and decode to PHP Array and then use string comparison.
Or maybe, you can give up the database way, just handling with a cache file. We're only need to write cache if user create/modify a post.
But you also can use the unreliable way, using like operator in mysql.
You should take a look at the MySQL fulltext index.
Take a look in the manual and this Zend Developer article
But you shouldn't use fulltext searching for many columns.
In one of my projects I worked around it by concatenating to be searched columns in a TEXT column and apply to the fulltext index on it.
It's simple you can try using like query
SELECT * FROM `post` WHERE `tags` LIKE '%html%';
In PHP Variable:
$tag = "html";
$query = mysql_query("SELECT * FROM `post` WHERE `tags` LIKE '%'.$tag.'%'");
I have a tricky (well for me it is) question about getting info from 2 different tables.
I have an array, like this:
$abc=$_COOKIE['cookie'];
$comma_separated = implode(",", $abc);
Now, the array will be a CSV list of ID's that need to match ID's from the "Specials" table. In the "Specials" table there is a column called Contract Name. This Contract Name column needs to match a column of the same name, each unique, in the Products table, and display the information contained therein.
My currenty MySQL query looks like this:
$query= "SELECT specials.id,
specials.product_name,
specials.contract_name,
specials.included_value AS inc_val,
specials.image_url,
specials.contract_monthly,
specials.outlet,
products.package,
products.bundled,
products.included_value
FROM specials,
product
WHERE `id` IN ('.$comma_separated.')
AND specials.contract_name = `products.package`";
What happens is... that nothing happens. I've tried wrapping my brain around some of the JOIN tutorials but no luck.
So basically I'd like to display a list of current specials, along with the package info, which is contained in a different table. I've tried wrapping my brain around some of the JOIN tutorials but no luck.
To my knowledge I'm crap at explaining things properly, so please do shout if I can shed any more light on this conundrum.
Thanks! :)
To even start to get this to work, your $query string is going to have to be a valid query. Ordinarily, to troubleshoot this kind of problem, you do echo $query; somewhere along the way to see if it is valid. You then might even paste the query's textinto a standalone MySQL client (phpMyAdmin, or maybe HeidiSQL or something) to see what you get.
Looking at this line:
WHERE `id` IN ('.$comma_separated.') /* wrong! */
it looks like it needs to read
WHERE `id` IN (".$comma_separated.")
because you're using double quotes to surround your fragments of SQL text. Also, you might want to use
WHERE `specials`.`id` IN (".$comma_separated.")
just because some other tables might contain id columns and then your search clause will be ambiguous.
Currently, I have a log file of messages in one table in a MySQL database. Among some other stuff, it contains the sender id, and the message itself. I want to make a way to display the log on a website.
I have a separate table that contains the sender ids and the name of the person (which is what I actually want to display).
Are there any better ways than simply running another query? While that would work, that's pretty expensive as it requires a new query for every entry. Ideally, I'd like something that would map the id --> name in an array, but I can only find things that will put everything from one row into an array (aka, horizontally), but I need entries "vertically".
I'm using PHP by the way...
Thanks!
Kevin
Learn about JOIN statements. This is exactly what you need.
I believe you are looking for something like this:
SELECT `name`, `message` FROM `msgtable` INNER JOIN `sendertable` USING(`sender_id`)
I have a table with about 150 websites listed in it with the columns "site_name", "visible_name" (basically a formatted name), and "description." For a given page on my site, I want to pull site_name and visible_name for every site in the table, and I want to pull all three columns for the selected site, which comes from the $_GET array (a URL parameter).
Right now I'm using 2 queries to do this, one that says "Get site_name and visible_name for all sites" and another that says "Get all 3 fields for one specific site." I'm guess a better way to do it is:
SELECT * FROM site_list;
thus reducing to 1 query, and then doing the rest post-query, which brings up 2 questions:
The "description" field for each site is about 200-300 characters. Is it bad from a performance standpoint to pull this for all 150 sites if I'm only using it for 1 site?
How do I reference the specific row from the MySQL result set for the site specificed in the URL? For example, if the URL is "mysite.com/results?site_name=foo" how do I do the post-query equivalent of SELECT * FROM site_list where site_name=foo; ?
I don't know how to get the data for "site_name=foo" without looping through the entire result array and checking to see if site_name matches the URL parameter. Isn't there a more efficient way to do it?
Thanks,
Chris
PS: I noticed a similar question on stackoverflow and read through all the answers but it didn't help in my situation, which is why I'm posting this.
Thanks,
Chris
I believe what you do now, keeping sperated queries for getting a list of sites with just titles and one detailed view with description for a single given site, is good. You don't pull any unneeded data and both queries being very simple are fast.
It is possible to combine both your queries into one, using left join, something maybe like:
SELECT s1.site_name, s1.visible_name, s2.description
FROM site_list s1
LEFT JOIN
( SELECT site_name, description
FROM site_list
WHERE site_name = 'this site should go with description' ) s2
ON s2.site_name = s1.site_name
resulting in all sites without matching name having NULL as description, you could even sort it using
ORDER BY description DESC, site_name
to get the site with description as first fetched row, thus eliminating need to iterate through results to find it, but mysql would have to do a lot more work to give you this result, negating any possible gain you could hope for. So basically stick to what you have now, its good.
Generally, it's good practice to have an 'id' field in the table as an auto_increment value. Then, you would:
SELECT id,url,display_name FROM table;
and you'd have the 'id' value there to later:
SELECT * FROM table WHERE id=123;
That's probably your most efficient method if you had WAAAY more entries in the table.
However, with only 150 rows in the table, you're probably just fine doing
SELECT * FROM table;
and only accessing that last field for a matching row based on your criteria.
If you only need the description for the site named foo you could just query the database with SELECT * FROM site_list WHERE site_name = 'foo' LIMIT 1
Otherwise you would have to loop though the result array and do a string comparison on site_name to find the correct description.
This is a bit of a difficult problem for me to word, and I may be going about it in the completely wrong way.
I'm storing a set of options in a database, where each option is its own column. The user can change the number of options, however, so I need a way of allowing PHP to always select all the options.
Let's say I have these columns: options_dialog_1, options_dialog_2, options_dialog_3, options_dialog_4
There could be a varying number of these dialog option columns, eg, another called options_dialog_5 could be added.
How do I select all the dialog option columns, based on their column name format?
I think you have a database design problem here; repeating columns like that always leads to trouble in the end. I think you need two tables, one for the user and one for the options defined something like this...
USERS
id
name
OPTIONS
id
user_id
option_dialogue_number
option_dialogue_value
That turns the columns into rows, which are rather easier to get at.
Brian's answer will really, really pay you off in longer period. But if you need something quick & ugly, you can check out the "metadata dictionary" (tables that store information about all other tables, columns etc). You could get list of columns from it with first query and use it to build the second one.
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='mytable' AND COLUMN_NAME LIKE 'options_dialog%'
Visit the manual on INFORMATION_SCHEMA for more goodies.
I am not sure I understand the problem. Are you looking for
SELECT * FROM options_table
Something like (faux SQL - wont work)
SELECT ( SELECT column_names where column_name LIKE 'options_dialog%' )
FROM options_table
sounds not feasible to me (though I am sure it's possible somehow). If you need this, either consider refactoring the database design or maybe use a bitmask to store the selected options in a single column.