2

I have a query like this:

SELECT COALESCE(MAX(german),"DEFAULT") from translate_content WHERE english = :englishContent0 UNION ALL
SELECT COALESCE(MAX(german),"DEFAULT") from translate_content WHERE english = :englishContent1 UNION ALL
SELECT COALESCE(MAX(german),"DEFAULT") from translate_content WHERE english = :englishContent2

With the idea that if a value is not found then it will return the value "DEFAULT". My problem is that this query works fine when I run it directly in the database, but when I try it in PHP I get an error saying "undefined index: "german".

I am using MySQL and PHP 5.5.11. I'm sorry if this isn't enough information, but I don't know what else to provide (but ask and I will deliver!)

I should probably state that the column "german" definitely does exist in the table "translate_content", and I have tried encapsulating the column names in back ticks. The problem only seems to arise when I use the COALESCE function, but I'm at a loss as to how it can work when I copy and paste it into the database, but not when I run it directly from PHP :/

Any help would be greatly appreciated, thanks.

3
  • Show us the code where you execute the queries (the php code). Commented Sep 22, 2015 at 12:26
  • 2
    You probably need to use as <column alias> to give the column a name that can then be referenced in PHP. Commented Sep 22, 2015 at 12:26
  • @GordonLinoff - thank you! I knew it must be something trivial, but of course it's only obvious once you know it. Thanks for the fast reply :) Commented Sep 22, 2015 at 12:30

1 Answer 1

2

use a column alias like

SELECT COALESCE(MAX(german),"DEFAULT") AS Column1

BTW, your query can be modified using a IN operator like below and no need of using UNION ALL

SELECT COALESCE(MAX(german),"DEFAULT") AS some_column
from translate_content WHERE english IN (:englishContent0,:englishContent1,:englishContent2)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks - I have just tried this thanks to Gordon's suggestion and it works :) I actually can't use IN in this instance due to the way the queries are generated, but thanks for the tip!

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.