問題描述
將 :name
形式的命名參數傳遞給 PDOStatement::bindParam()
,不管是否使用前導冒號似乎都有效.
When passing named parameters of the form :name
to PDOStatement::bindParam()
, it seems to work whether or not the leading colon is used.
即要么這樣:
$statement->bindParam(':name', $var);
或者這個:
$statement->bindParam('name', $var);
似乎有效.
這里是 PDOStatement::bindParam()
參數
參數標識符.對于使用命名的準備好的語句占位符,這將是 :name. 形式的參數名稱.使用問號占位符準備好的語句,這將是1-參數的索引位置.
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.
但是,它確實可以正常工作(至少在 PHP 5.3.24 中) - 如果缺少冒號,內部會在參數中添加一個冒號(參見 PHP 5.3.24 中的ext/pdo/pdo_stmt.c:363
源代碼).
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() 的參數名稱的前導冒號是可選的嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!