参数名的前导冒号是否传递给PDOStatement :: bindParam()是可选的? [英] Is the leading colon for parameter names passed to PDOStatement::bindParam() optional?

查看:111
本文介绍了参数名的前导冒号是否传递给PDOStatement :: bindParam()是可选的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将:name形式的命名参数传递给 PDOStatement::bindParam() 时,不管是否使用前导冒号似乎都行得通.

即要么:

$statement->bindParam(':name', $var);

或者这个:

$statement->bindParam('name', $var);

似乎可以正常工作.

以下是 PDOStatement::bindParam()

的文档

参数

参数标识符.对于使用named的准备好的语句 占位符,这将是格式为:name的参数名称. 使用问号占位符的准备好的语句,这将是 参数的1分度位置.

这是否意味着可以将结肠排除在外?

解决方案

不,由于文档中未提及此内容,因此我认为可以肯定地认为这种行为不受官方支持并且不应被依赖. /p>

但是,它实际上确实起作用了(至少在PHP 5.3.24中有效)-如果缺少冒号,则会在内部将其添加到参数中(PDOStatement::bindParam(), it seems to work whether or not the leading colon is used.

i.e. either this:

$statement->bindParam(':name', $var);

or this:

$statement->bindParam('name', $var);

seems to work.

Here's the documentation for PDOStatement::bindParam()

parameter

Parameter identifier. For a prepared statement using named placeholders, this will be a parameter name of the form :name. For a prepared statement using question mark placeholders, this will be the 1-indexed position of the parameter.

Does this mean the colon can be left off?

解决方案

No, since the documentation doesn't mention this I think it's safe to assume that this behaviour isn't officially supported and shouldn't be relied upon.

However, it does actually happen to work (in PHP 5.3.24 at least) - internally a colon will be added to the parameter if it's missing (see ext/pdo/pdo_stmt.c:363 in the PHP 5.3.24 source code).

这篇关于参数名的前导冒号是否传递给PDOStatement :: bindParam()是可选的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆