PHP 和报价

标签 php mysql

我总是不确定,而且我发现自己经常玩弄它。

这样合适吗?

$game_name = "poker"; (Included from Config.php)
$game_name_lower = strtolower($game_name);
$sql = "SELECT * FROM `winners` WHERE `game` = '$game_name_lower'";

最佳答案

是的,看起来不错。但是,您可能需要考虑使用准备好的语句。引用 PHP Manual on the topic :

Prepared statements offer two major benefits:

  • The query only needs to be parsed (or prepared) once, but can be executed multiple times with the same or different parameters. When the query is prepared, the database will analyze, compile and optimize it's plan for executing the query. For complex queries this process can take up enough time that it will noticeably slow down an application if there is a need to repeat the same query many times with different parameters. By using a prepared statement the application avoids repeating the analyze/compile/optimize cycle. This means that prepared statements use fewer resources and thus run faster.

  • The parameters to prepared statements don't need to be quoted; the driver automatically handles this. If an application exclusively uses prepared statements, the developer can be sure that no SQL injection will occur (however, if other portions of the query are being built up with unescaped input, SQL injection is still possible).

关于PHP 和报价,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3510514/

相关文章:

php - RegExp 查找中间带有随机字符的特定字符串

mysql - php/mysql 编码不工作

php - 使用 mysqli_stmt_bind_result 时遇到问题

php - 如何获得最深的XML元素

php - 从数据库中选择具有相同 ID 的最小数量

php - 无效使用群组功能

php - 我应该使用多个mysql数据库表来存储php数组数据吗?

php - SELECT id from 'persone' LEFT JOIN time on persone.id=time.p' at line 2

PHP 系列适用于 Windows,但不适用于 Linux

php - Yii如何美化 Assets URL?