Avatar of MDauphinais1
MDauphinais1 asked on

String multiple values in WHERE clause

I have a php variable "$song[0]" that is already formatted like this: "Artist - Title".

I need to search for a matching record within a database that stores the artist and titles in seperate columns. To do this, I created this query:

"SELECT s.id, s.artist, s.title, s.picture, s.buycd, s.label, s.composer, s.rating, s.numvotes, s.duration, s.date_played FROM songlist s WHERE ((s.artist & ' - ' & s.title) = '$song[0]') LIMIT 0,1"

The problem is that instead of pulling the correct record, it just pulls the first record in the table. What is the correct way to write this?
PHPMySQL Server

Avatar of undefined
Last Comment
Member_2_4694817

8/22/2022 - Mon
reb73

Change the '&' to a '+' as follows -

SELECT s.id, s.artist, s.title, s.picture, s.buycd, s.label, s.composer, s.rating, s.numvotes, s.duration, s.date_played FROM songlist s WHERE ((s.artist + ' - ' + s.title) = '$song[0]') LIMIT 0,1
ASKER CERTIFIED SOLUTION
hielo

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Daniel Wilson

Can you echo the assembled SQL statement?  Likely it's not getting put together the way you intend ...
ASKER
MDauphinais1

Thanks!
Your help has saved me hundreds of hours of internet surfing.
fblack61
Bruce Pieterse

Hi,

Remove LIMIT 0,1 at the end of the SQL statement.

Cheers
Member_2_4694817


"SELECT id, artist, title, picture, buycd, label, composer, rating, numvotes, duration, date_played FROM songlist
WHERE CONCAT(artist,' - ',title) = '{$song[0]}'"

or better


list($artist, $title) = split(' - ',$song[0], 2);
$result = mysq_query("SELECT id, artist, title, picture, buycd, label, composer, rating, numvotes, duration, date_played FROM songlist WHERE artist='" . mysql_real_escape_string($artist) ."' AND title='" . mysql_real_escape_string($title) . '"';

Open in new window