【问题标题】:Reusing Sprintf arguments or an alternative in PHP在 PHP 中重用 Sprintf 参数或替代方法
【发布时间】:2014-07-02 16:36:34
【问题描述】:

我有一个很长的UNION,下面只是一个sn-p的查询:

UNION 
(
    /* Get new contact requests to you */
        SELECT r.registered, r.id, u.x_account_username, r.message AS comment, '' AS name, 'new_contact_request' AS type
            FROM x_allies r
            LEFT JOIN x_user u
            ON r.from = u.id
            WHERE r.registered BETWEEN '%s' AND '%s' AND r.to = '%s'
)

我一直在使用 sprintf 函数将正确的值插入到我的查询中。

但是,我遇到了一个问题。

我的 sprintf 现在看起来像这样..

如何在 sprintf 函数中重复使用参数?

$query      =   sprintf($query, $c, $t, $c, $a, $c, $e, $profile_id, $profile_id, $profile_id, $tryblimp, $now, $profile_id, $c, $tryblimp, $now, $profile_id, $c, $tryblimp, $now, $profile_id, $c, $tryblimp);

I have seen implementations of re-using arguments within PHP, but I can't seem to get it working. Here is what I have found already.

你能帮我找到解决办法吗?

谢谢

【问题讨论】:

    标签: php mysql arguments printf


    【解决方案1】:

    将值直接插入 SQL 查询字符串是一种非常糟糕的做法,因为您应该非常小心参数引用。否则这可能会导致 SQL 注入或只是 SQL 错误。 我建议你使用 PDO 语句和parameters,如下所示:

    $query = 'Select * FROM table WHERE registered BETWEEN :begin AND :end';
    $parametersValues = array(':begin' => $bedigDate, ':end' => $endDate);
    $statement = $db->prepare($query);
    $statement->execute($parametersValues);
    $result = $statement->fetchAll();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-09
      • 2014-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-12
      • 1970-01-01
      相关资源
      最近更新 更多