0

I am trying to run a MySQL query from a PHP script like this:

$q = "
UPDATE
  cf_ab_companies, inndata200
SET
  cf_ab_companies.col_158 = inndata200.AFakt_10_Fast_renhold, 
  cf_ab_companies.col_159 = IF ( inndata200.Startdato = "01.01.3000 00:00" OR inndata200.Startdato = "01.01.2000 00:00", NULL, str_to_date(inndata200.Startdato, '%d.%m.%Y')), 
  cf_ab_companies.col_160 = IF ( inndata200.Sluttdato = "01.01.3000 00:00" OR inndata200.Sluttdato = "01.01.2000 00:00", NULL, str_to_date(inndata200.Sluttdato, '%d.%m.%Y'))
WHERE
  cf_ab_companies.model_id = inndata200.ImportGOID;";

 mysql_query($q, $db);

But it seems that the query is not stored properly in PHP. What is the simplest way to store queries like this in PHP?

Thanks for all help

5
  • What do you mean by "store queries"? Commented Nov 28, 2013 at 14:31
  • btw: your datetime stamps in your query should be surrounded by '' and not "" Commented Nov 28, 2013 at 14:33
  • php should show you an error the way you wrote it here, because you have unescaped " in your string (e.g. here inndata200.Startdato = "01.01.3000 00:00" ). So you need to at least escape them (write them as \") or use ' instead. Commented Nov 28, 2013 at 14:33
  • The query above works as I want in phpmyadmin. But instead I want to perform the query from a PHP script... Commented Nov 28, 2013 at 14:33
  • put the values to be set in qouts(") Commented Nov 28, 2013 at 14:33

2 Answers 2

1

This should make it work; put slashes (\) before your quotes (") because it gets confused:

$q = "
UPDATE
  cf_ab_companies, inndata200
SET
  cf_ab_companies.col_158 = inndata200.AFakt_10_Fast_renhold, 
  cf_ab_companies.col_159 = IF ( inndata200.Startdato = \"01.01.3000 00:00\" OR inndata200.Startdato = \"01.01.2000 00:00\", NULL, str_to_date(inndata200.Startdato, '%d.%m.%Y')), 
  cf_ab_companies.col_160 = IF ( inndata200.Sluttdato = \"01.01.3000 00:00\" OR inndata200.Sluttdato = \"01.01.2000 00:00\", NULL, str_to_date(inndata200.Sluttdato, '%d.%m.%Y'))
WHERE
  cf_ab_companies.model_id = inndata200.ImportGOID;";
Sign up to request clarification or add additional context in comments.

Comments

0

I managed to solve it by following the advice by t.niese to escape the "'s

Thanks a lot!

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.