Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
223e259b
Commit
223e259b
authored
Jul 03, 2013
by
Alexander Makarov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
prioritized quoting using special syntax instead of doing it manually
parent
33c36f2a
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
10 additions
and
11 deletions
+10
-11
database-basics.md
docs/guide/database-basics.md
+10
-11
No files found.
docs/guide/database-basics.md
View file @
223e259b
...
...
@@ -133,17 +133,7 @@ $connection->createCommand()->delete('tbl_user', 'status = 0')->execute();
Quoting table and column names
------------------------------
If you are building query string dynamically make sure you're properly quoting table and column names using
[
[\yii\db\Connection::quoteTableName()]] and [[\yii\db\Connection::quoteColumnName()]
]:
```php
$column = $connection->quoteColumnName($column);
$table = $connection->quoteTableName($table);
$sql = "SELECT COUNT($column) FROM $table";
$rowCount = $connection->createCommand($sql)->queryScalar();
```
Alternatively you can use special syntax when writing SQL:
Most of the time you would use the following syntax for quoting table and column names:
```
php
$sql
=
"SELECT COUNT(
{
{$column}
}
) FROM [[
$table
]]"
;
...
...
@@ -153,6 +143,15 @@ $rowCount = $connection->createCommand($sql)->queryScalar();
In the code above
`{{X}}`
will be converted to properly quoted column name while
`[[Y]]`
will be converted to properly
quoted table name.
The alternative is to quote table and column names manually using
[
[\yii\db\Connection::quoteTableName()
]
] and
[
[\yii\db\Connection::quoteColumnName()]
]:
```php
$column = $connection->quoteColumnName($column);
$table = $connection->quoteTableName($table);
$sql = "SELECT COUNT($column) FROM $table";
$rowCount = $connection->createCommand($sql)->queryScalar();
```
Prepared statements
-------------------
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment