【问题标题】:Concat Help: Adding string to Query in mysql/phpConcat 帮助:在 mysql/php 中将字符串添加到 Query
【发布时间】:2011-08-29 01:58:42
【问题描述】:

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

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

每次我尝试运行该文件时,都会收到一条错误消息“(!) Parse error: syntax error, unexpected T_STRING in C:\wamp\www\output.php on line 22”。我知道该查询在我的 sequel pro 中有效,但是当我尝试将它合并到实际文件中时,它会归档

【问题讨论】:

    标签: php mysql concatenation


    【解决方案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']),但总的来说这有点冒险在我看来(但对于您发布的代码看起来不错)。

    【讨论】:

    • 不幸的是,当我添加 $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";我收到以下结果:(!)注意:未定义索引:第 38 行 C:\wamp\www\output.php 中的标题我是否需要更改最后一个 fprintf 以反映此更改?
    • 是的。 concat 更改字段名称 - 我将编辑我的答案以帮助您。
    【解决方案2】:

    我对长字符串使用的一种有趣的方法是heredocs,以便弄清楚要使用哪种类型的引用。 Heredoc 示例如下所示:

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

    首先,&lt;&lt;&lt; 表示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 之类的操作,而无需依赖函数调用,则效果非常好。但是,对于较短的字符串,我建议使用标准字符串。

    【讨论】:

      猜你喜欢
      • 2011-05-13
      • 2012-01-26
      • 2015-07-26
      • 2013-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-19
      相关资源
      最近更新 更多