Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Want values from column from mysql database table in the form of list.

Hi, Thanks in advance. I am creating a search functionality in my project. I have entered keywords using form in the table. Every topics consists of some keywords in the table. I want to fetch all the words from all topics and want to compare it with the user search word. I am stuck at how to get the values from the column in the list form so that i can compare the value.

3 Answers

Jeff Lemay
Jeff Lemay
14,268 Points

Would something like this work for you?

SELECT id, movie, director
FROM mymoviestable
WHERE
(
    movie LIKE '%keyword%'
    OR director LIKE '%keyword%'
)

Hi, Thanks for the reply. I didn't get the code. My sql table looks like below topic_details - table name id keywords 1 exam, essential 2 storyline, before I want to select all keywords for id 1 and 2 as a list, so that i can compare with the user search field.

Jeff Lemay
Jeff Lemay
14,268 Points

I don't fully follow your table structure here..

The sample I posted will take your search query and compare it to two columns in your table (movie and director). You can change what is returned and table/column names to match your specific table. I'll give it a shot here:

SELECT *
FROM topic_details
WHERE
(
    exam LIKE '%query%'
    OR storyline LIKE '%query%'
)

SELECT * --- this will return all the data to you, you can manipulate after how you see fit. FROM topic_details --- we're targeting the topic_details table WHERE --- our parameters for only returning rows that match our search query exam LIKE '%query%' --- match any rows who's exam column contains our search query (the % signs are wildcards, so if you search 'foot', the terms 'barefoot' and 'footwear' would match) OR storyline LIKE '%query%' --- match any rows who's storyline column contains our search query

Hi, It's working. Thanks a lot.