简体   繁体   English

PHP MySQL将“ {$ clientname}”作为字符串而不是PHP变量插入

[英]PHP MySQL Insert “{$clientname}” as a string not PHP variable

Ok, first question so here goes, 好,第一个问题就这样,

A function that runs on activation of a module I am writing needs to put email templates into a MySQL db. 在我编写的模块激活时运行的功能需要将电子邮件模板放入MySQL数据库。 The problem is these are smarty templates and use {$vars}, now when I do this in PHP the {$vars} are interpreted as PHP variables to be put into the sql query before being run. 问题是这些模板很聪明,并且使用{$ vars},现在当我在PHP中执行此操作时,{$ vars}被解释为要在运行前放入sql查询中的PHP变量。

How do I tell PHP to not interpret "{$var}" as a variable but as a string? 如何告诉PHP不要将“ {$ var}”解释为变量而是字符串?

This is the current query: 这是当前查询:

$sqlEmail1 = "INSERT INTO `tblemailtemplates` (`type`, `name`, `subject`, `message`, `attachments`, `fromname`, `fromemail`, `disabled`, `custom`, `language`, `copyto`, `plaintext`)
VALUES ('general', 'IPGeek - Custom Email 1', 'Password Reset', 'Some text that contains {$company_name} among other things', '', '', '', '', '1', '', '', 0)"

And this should be the email template the above creates: 这应该是上面创建的电子邮件模板:

Some text that contains {$company_name} among other things

But it creates this instead: 但是它创建了这个:

Some text that contains 

Any help would be very gladly received! 任何帮助都会很高兴收到!

When a string is specified in double quotes or with heredoc, variables are parsed within it. 当用双引号或heredoc指定字符串时,将在其中解析变量。

Note: Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings. 注意:与双引号和heredoc语法不同,特殊字符的变量和转义序列在单引号引起来的字符串中不会扩展。

Use ' instead of " 使用'代替"

echo '{$var}';

Or escape $ : 或转义$

echo "{\$var}";

Learn more: php strings 了解更多: php字符串

UPDATE : Minimal effort fix is just to add \\ before $ : 更新 :最小努力修复只是在$之前添加\\

$sqlEmail1 = "INSERT INTO `tblemailtemplates` (`type`, `name`, `subject`, `message`, `attachments`, `fromname`, `fromemail`, `disabled`, `custom`, `language`, `copyto`, `plaintext`)
VALUES ('general', 'IPGeek - Custom Email 1', 'Password Reset', 'Some text that contains {\$company_name} among other things', '', '', '', '', '1', '', '', 0)";

How about escaping the values properly? 如何正确转义值?

I'm betting there are ' (apostrophes) in your templates. 我敢打赌,您的模板中有'(撇号)。 If so, it would break your SQL syntax. 如果是这样,它将破坏您的SQL语法。

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

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