php - 重用 Sprintf 参数或 PHP 中的替代方案

标签 php mysql arguments printf

我有一个很长的 UNION,下面只是查询的一个片段:

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.

你能帮我找到解决方案吗?

谢谢

最佳答案

将值直接插入到 SQL 查询字符串中是一种非常糟糕的做法,因为您应该非常小心参数引用。否则,这可能会导致 SQL 注入(inject)或只是 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();

关于php - 重用 Sprintf 参数或 PHP 中的替代方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23657762/

相关文章:

javascript - 使用 CryptoJS 解码 Base64 字符串

php - symfony2 命令中的自定义独白日志记录 channel

mysql - SQL-模式匹配无输出

php - Android Studio - 登录/注册卡在检查网络

php - 是否可以通过页面参数自动按下提交按钮?

python - Telegram 类型错误 : the first argument must be callable

php - 重写数据库值php

PHP 和 'variable' mySQL 查询

php - SQL中的控制流问题

javascript - 从头开始编码 JS Underscore _.extend