1

I've encountered a strange issue where my MySQL query works for all cases except for one particular data entry.

Here's my query:

SELECT * FROM wp_songs WHERE album_name = '{$current_album}'

This query is fed into the Wordpress $wpdb->get_results() function. For some reason it can never find this particular data entry: "Raw & Uncut: The Mixtape"

However, if the query is instead changed to:

 SELECT * FROM wp_songs WHERE album_name = 'Raw & Uncut: The Mixtape'

The data is found without a problem.

Running the $wpdb->last_query function returns the exact same line in both cases.

The issue is only for one data entry. I have not been able to replicate the problem with other data entries. All other entries work with my original query.

More code:

This does not work:

 $current_album = "Raw & Uncut: The Mixtape";
 $songs = $wpdb->get_results("SELECT * FROM wp_songs WHERE album_name = '{$current_album}'")
if ( $songs ) { echo "Works"; }

This works:

  $songs = $wpdb->get_results("SELECT * FROM wp_songs WHERE album_name = 'Raw & Uncut: The Mixtape'");
  if ( $songs ) { echo "Works"; }

Again, it's only for that specific data entry. Every other entry works with both queries.

Running var_dump($current_album) returns:

string(28) "Raw & Uncut: The Mixtape"
5
  • please show all the relevant code Commented Aug 3, 2012 at 0:02
  • try this construct with a string that does not contain special characters. I think th "&" and the ":" made the problems. Commented Aug 3, 2012 at 0:15
  • It works with a string that does not contain special characters, but it also works when I don't use a variable. Commented Aug 3, 2012 at 0:25
  • I think that's what Panique thought would happen. This would show that the there's a bug in the wordpress expansion routine. Commented Aug 3, 2012 at 0:36
  • try using like? SELECT * FROM wp_songs WHERE album_name LIKE '".$search."%' Commented Aug 3, 2012 at 1:11

1 Answer 1

3

You posted the results of var_dumping $current album:

string(28) "Raw & Uncut: The Mixtape"

"Raw & Uncut: The Mixtape" is only 24 characters - what that tells me is that your variable actually contains this:

Raw & Uncut: The Mixtape

Which doesn't match what you have in the database. You can correct it by running html_entity_decode() on your album variable.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.