0

I have a custom table in my Wordpress database and have a custom query that runs against it and displays the data in a custom template.

This is the query

 $query ="
                        SELECT *
                        FROM cp
                        WHERE year = $cp_year
                        AND quarter = $cp_quarter
                        AND volume = $cp_volume
                        AND page <= $cp_page
                        ORDER BY page DESC
                        LIMIT 1
                        ";

This works fine as long as $cp_volume is an integer but if it is a string variable such as 1a the query fails with this error message

WordPress database error: [Unknown column '2a' in 'where clause'] SELECT * FROM cp WHERE year = 1911 AND quarter = 4 AND volume = 2a AND page <= 1475 ORDER BY page DESC LIMIT 1

volume is declared as varchar in the database table.

I have been struggling and searching on Google for several hours without success. Anyone got any bright ideas please?

3
  • 1
    on volume pass single quote in parameter. Now your query is SELECT * FROM cp WHERE year = '1911' AND quarter = '4' AND volume = '2a' AND page <= '1475' ORDER BY page DESC LIMIT 1 Try this code Commented Feb 21, 2015 at 11:27
  • Thanks that works now. I think I will add single quotes to one of the other variables too as that may contain letters too.
    – Colin
    Commented Feb 21, 2015 at 11:42
  • Yes Exactly ... So don't remember to give points in right Answer. Commented Feb 21, 2015 at 11:43

1 Answer 1

0

On volume pass single quote in parameter. Now your query is

SELECT * FROM cp WHERE year = '1911' AND quarter = '4' AND volume = '2a' AND page <= '1475' ORDER BY page DESC LIMIT 1   Try this code

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.