What does a colon before a literal in an SQL state

2019-01-26 11:17发布

What does it mean to use ":" before a variable ?

For example, :userId in this code:

public function removeUser($userId)
{
 $command = Yii::app()->db->createCommand();
 $command->delete(
 'tbl_project_user_assignment',
 'user_id=:userId AND project_id=:projectId',
 array(':userId'=>$userId,':projectId'=>$this->id));
}

This is PHP,MySQL code in Yii framework.

标签: php mysql yii
3条回答
别忘想泡老子
2楼-- · 2019-01-26 11:45

As thaidiotguy mentions, it's a character commenly used with prepared statements especially with PDO. In PDO the colon tells that the following is a named parameter.

查看更多
Ridiculous、
3楼-- · 2019-01-26 11:53

:userId is a placeholder

According to Yii's documentation for SQL statement:

For a prepared statement using named placeholders, this will be a parameter name of the form :name.

查看更多
做自己的国王
4楼-- · 2019-01-26 12:04

The colon is a common character that indicates a placeholder for a variable value in a SQL statement. In this case, the those placeholders are getting replaced by the value of userId and project_id at runtime. This is great for avoiding SQL injection vulnerabilities.

查看更多
登录 后发表回答