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
Avatar of reb73
reb73
Flag of Ireland image

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
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Daniel Wilson
Daniel Wilson
Flag of United States of America image

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

ASKER

Thanks!
Hi,

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

Cheers

"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

PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo