简体   繁体   English

从php运行mysql查询

[英]Run mysql query from php

I am trying to run a MySQL query from a PHP script like this: 我试图从这样的PHP脚本运行MySQL查询:

$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. 但似乎查询未正确存储在PHP中。 What is the simplest way to store queries like this in PHP? 在PHP中存储这样的查询的最简单方法是什么?

Thanks for all help 感谢所有帮助

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;";

I managed to solve it by following the advice by t.niese to escape the "'s 我设法按照t.neese的建议解决了这个问题,

Thanks a lot! 非常感谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM