我有一个很长的 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);
你能帮我找到解决方案吗?
谢谢
最佳答案
将值直接插入到 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/