-2

Previously, this was working:

$patient_story_set_photos = $wpdb->get_results('SELECT * FROM wp_before_after WHERE patientID = '.$post->ID.' AND patient_display = 1');

However, when I try to add another AND condition like this:

$patient_story_set_photos = $wpdb->get_results('SELECT * FROM wp_before_after WHERE patientID = '.$post->ID.' AND patient_display = 1 AND period_taken = '.$set->period_taken);

I get the following error on screen:

WordPress database error: [Unknown column '1hour' in 'where clause']
SELECT * FROM wp_before_after WHERE patientID = 8175 AND patient_display = 1 AND period_taken = 1hour

Can't see why there's a problem, are you not allowed to use multiple AND conditions in SQL?

Lee
  • 4,187
  • 6
  • 25
  • 71

2 Answers2

0

The problem is not the AND, the problem is your 1hour, 1hour unquoted means a reference to an object (database, table) named 1hour, you need to quote '1hour'. If you write

    SELECT * FROM wp_before_after 
WHERE patientID = 8175 
AND patient_display = 1 
AND period_taken = '1hour'

you will compare the field periodtaken to a string (CHAR,VARCHAR,TEXT) equal to '1hour'. I assume period_taken is a field typed CHAR,VARCHAR or TEXT

0

Before anything, DO NOT CONCATENATE SQL STRINGS nowadays it is a MUST (see how to do it properly https://stackoverflow.com/a/60496/3771219)

The problem you are facing is because, I presume, that the period_taken field is some sort of Char/Varchar/String field and when you are filtering by a "Stringy" field you must sorround your literals values with single quotes:

SELECT * 
FROM wp_before_after 
WHERE patientID = 8175 
AND patient_display = 1 
AND period_taken = '1hour'

Hope this help

Asero82
  • 111
  • 3