Concat帮助:在mysql / php中向Query添加字符串

时间:2011-05-19 17:27:22

标签: php mysql concatenation

我以为我可以自己解决这个问题,但显然我正在努力解决这个问题。我最初有以下代码:

 $query = "SELECT cards.card_id,title,description,meta_description,seo_keywords,price FROM cards,card_cheapest WHERE cards.card_id = card_cheapest.card_id ORDER BY card_id";
$result = mysql_query($query);


// Open file for writing
$myFile = "googleproducts.txt";
$fh = fopen($myFile, 'w') or die("can't open file");

// Loop through returned data and write (append) directly to file
fprintf($fh, "%-200s %-200s  %-800s   %-200s %-800s\n", "id", "label","description","price","seo keywords");
fprintf($fh, "\n");
while ($row = mysql_fetch_assoc($result)) {
 fprintf($fh, "%-200s  %-200s  %-800s  %-200s  %-800s\n", $row['card_id'], $row['title'], $row['description'],$row['price'], $row['seo_keywords']);
}

// Close out the file
fclose($fh);
?>

当文件打印到文本文件时,我需要做的是将“By Amy”添加到标题中,所以我想我会像这样对其进行调整:

$query = "SELECT cards.card_id,concat(title, "By Amy"),description,meta_description,seo_keywords,price FROM cards,card_cheapest WHERE cards.card_id = card_cheapest.card_id ORDER BY card_id";

每次我都会尝试运行该文件,我会收到一条错误,说“(!)解析错误:语法错误,第22行的C:\ wamp \ www \ output.php中的意外T_STRING”。我知道这个查询在我的续集专业版中有效,但是当我尝试将它合并到实际文件中时,它就是文件

2 个答案:

答案 0 :(得分:1)

这是因为你在另一个双引号字符串(SELECT ... card_id)中包含一个双引号字符串(By Amy)。

PHP解析器无法理解发生了什么,因此您甚至在查询被发送到数据库之前就会收到该错误。

通过将两个内部“更改为”

来转义内部字符串

例如:

$query = "SELECT cards.card_id, concat(title, "By Amy"), 
    description, meta_description, 
    seo_keywords, price 
FROM cards, card_cheapest 
WHERE cards.card_id = card_cheapest.card_id 
ORDER BY card_id";

$query = "SELECT cards.card_id, concat(title, \"By Amy\") AS TitleConcat, 
    description, meta_description, 
    seo_keywords, price 
FROM cards, card_cheapest 
WHERE cards.card_id = card_cheapest.card_id 
ORDER BY card_id";

编辑:注意在concat字段后添加AS TitleConcat。这会将列重命名为TitleConcat,然后您可以通过$row['TitleConcat'].

访问它

另一种方法是使用AS title,并且更改对代码的其余部分是透明的(也就是说,您不必将$row['title']更改为$row['TitleConcat']),但是一般来说,这在我看来有点风险(但对于您发布的代码看起来很好)。

答案 1 :(得分:0)

我用一个有趣的方法来查找长字符串以找出要使用的引用类型heredocs。一个样本heredoc看起来像这样:

$string = <<<STRING
Lots of text
'this works'
"and this"
You can even use $variable expansion
STRING;

首先,<<<表示heredoc的开始。然后传递一个名称以标识多行字符串停止的位置。在这种情况下,我们使用STRING。但是,您可以使用您想要的任何名称。使用heredocs的一个缺点是,为了告诉php您的多行字符串已完成,您必须使用相同的标识符名称(带分号)作为该行的第一个条目。所以这个:

$string = <<<STRING
Lots of text
'this works'
"and this"
You can even use $variable expansion
    STRING; // This won't do what you expect it to

不会起作用,因为它会占据空间。你也不能突破调用函数:

$string = <<<STRING
Lots of text
'this works'
"and this"
You can even use $variable expansion
my_function_call() <-- this gets interpreted as text, not the result of the function
STRING; // This won't do what you expect it to

考虑你的例子:

$query = <<<SQL
SELECT cards.card_id,concat(title, "By Amy"),description,meta_description,seo_keywords,price
FROM cards,card_cheapest 
WHERE cards.card_id = card_cheapest.card_id 
ORDER BY card_id
SQL;

因此,如果您正在执行输出大量SQL的操作,而不需要依赖函数调用,那么这非常合适。对于较短的字符串,我建议使用标准字符串。