bindValue() 和 bindParam() 之间的混淆?

Confusion between bindValue() and bindParam()?(bindValue() 和 bindParam() 之间的混淆?)
本文介绍了bindValue() 和 bindParam() 之间的混淆?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我混淆了这两个函数 Bindvalue()BindParam()

I am confuse between these two functions Bindvalue() and BindParam()

  1. 我在 php.net 上看到它不会转义 %_,所以在使用 LIKE 时要小心.所以我认为当我们使用 LIKE 查询时不使用 BindValue().
  2. 当我们使用LIKE 查询BindParam() 被使用.因为据我所知 BindParam 可以转义这些 %_.
  3. BindValue() 不提供针对 sql 注入的保护.我不确定这一点,是真的吗?
  1. I read on php.net it does not escape % and _, so be careful when using LIKE. So i think BindValue() is not used when we are using LIKE query.
  2. when we using LIKE query BindParam() is used. Because as i know BindParam can escape these % and _.
  3. BindValue() doesn't gives protection against sql injection. I am not sure about this, is it true?

朋友们说说我在这3点中所说的对与错.我是 PDO 的初学者,所以请解释清楚..

friends tell what i mention in these 3 points is right or wrong. i am beginner in PDO so please explain it clearly ..

推荐答案

值的转义方式和不转义方式应该没有区别.bindParambindValue 的不同之处在于它引用变量,仅在执行语句时绑定值.bindValue 立即获取 value.举例说明:

There should be no difference in how values are escaped or not escaped. bindParam differs from bindValue in that it references the variable, binding the value only when you execute the statement. bindValue takes the value immediately. To illustrate:

$stmt = $db->prepare('SELECT * FROM `table` WHERE foo = :foo');

$foo = 'foo';
$stmt->bindValue(':foo', $foo);
$foo = 'bar';

$stmt->execute();

上面的执行类似于 SELECT * FROM table WHERE foo = 'foo';

$stmt = $db->prepare('SELECT * FROM `table` WHERE foo = :foo');

$foo = 'foo';
$stmt->bindParam(':foo', $foo);
$foo = 'bar';

$stmt->execute()

上面的执行类似于 SELECT * FROM table WHERE foo = 'bar'.

确实没有将_%当成特殊字符,因为一般来说,就语法而言,它们不是特殊字符,数据库驱动程序无法分析上下文来确定您是 mean % 是通配符还是 LIKE<上下文中的实际字符%"/code> 查询.

It's true that neither cares about _ or % as special characters, because generally speaking they aren't special characters as far as the syntax is concerned, and the database driver is not able to analyze the context to figure out whether you mean % to be a wildcard or the actual character "%" in the context of a LIKE query.

两者都可以防止 SQL 注入.

Both protect against SQL injection.

这篇关于bindValue() 和 bindParam() 之间的混淆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)