перевод sql query на подготовленные запросы

This commit is contained in:
rsgrinko committed 2026-07-24 19:17:07 +03:00
1 parent 322ea1d69f
commit 83c2d29f68
14 files changed
+1025 -1166

No files matched your search

+1 -5
View File
@@ -57,7 +57,6 @@
<excludeFolder url="file://$MODULE_DIR$/vendor/symfony/polyfill-php80" />
<excludeFolder url="file://$MODULE_DIR$/vendor/symfony/service-contracts" />
<excludeFolder url="file://$MODULE_DIR$/vendor/symfony/polyfill-mbstring" />
<excludeFolder url="file://$MODULE_DIR$/vendor/symfony/polyfill-php73" />
<excludeFolder url="file://$MODULE_DIR$/vendor/symfony/string" />
<excludeFolder url="file://$MODULE_DIR$/vendor/doctrine/instantiator" />
<excludeFolder url="file://$MODULE_DIR$/vendor/myclabs/deep-copy" />
@@ -87,16 +86,13 @@
<excludeFolder url="file://$MODULE_DIR$/vendor/theseer/tokenizer" />
<excludeFolder url="file://$MODULE_DIR$/vendor/psr/simple-cache" />
<excludeFolder url="file://$MODULE_DIR$/vendor/symfony/filesystem" />
<excludeFolder url="file://$MODULE_DIR$/vendor/symfony/polyfill-php81" />
<excludeFolder url="file://$MODULE_DIR$/vendor/symfony/config" />
<excludeFolder url="file://$MODULE_DIR$/vendor/cakephp/database" />
<excludeFolder url="file://$MODULE_DIR$/vendor/cakephp/datasource" />
<excludeFolder url="file://$MODULE_DIR$/vendor/cakephp/core" />
<excludeFolder url="file://$MODULE_DIR$/vendor/cakephp/utility" />
<excludeFolder url="file://$MODULE_DIR$/vendor/robmorgan/phinx" />
<excludeFolder url="file://$MODULE_DIR$/vendor/graham-campbell/result-type" />
<excludeFolder url="file://$MODULE_DIR$/vendor/phpoption/phpoption" />
<excludeFolder url="file://$MODULE_DIR$/vendor/vlucas/phpdotenv" />
<excludeFolder url="file://$MODULE_DIR$/vendor/symfony/polyfill-php83" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
Generated
+1 -5
View File
@@ -65,7 +65,6 @@
<path value="$PROJECT_DIR$/vendor/symfony/polyfill-php80" />
<path value="$PROJECT_DIR$/vendor/symfony/service-contracts" />
<path value="$PROJECT_DIR$/vendor/symfony/polyfill-mbstring" />
<path value="$PROJECT_DIR$/vendor/symfony/polyfill-php73" />
<path value="$PROJECT_DIR$/vendor/symfony/string" />
<path value="$PROJECT_DIR$/vendor/doctrine/instantiator" />
<path value="$PROJECT_DIR$/vendor/myclabs/deep-copy" />
@@ -95,16 +94,13 @@
<path value="$PROJECT_DIR$/vendor/theseer/tokenizer" />
<path value="$PROJECT_DIR$/vendor/psr/simple-cache" />
<path value="$PROJECT_DIR$/vendor/symfony/filesystem" />
<path value="$PROJECT_DIR$/vendor/symfony/polyfill-php81" />
<path value="$PROJECT_DIR$/vendor/symfony/config" />
<path value="$PROJECT_DIR$/vendor/cakephp/database" />
<path value="$PROJECT_DIR$/vendor/cakephp/datasource" />
<path value="$PROJECT_DIR$/vendor/cakephp/core" />
<path value="$PROJECT_DIR$/vendor/cakephp/utility" />
<path value="$PROJECT_DIR$/vendor/robmorgan/phinx" />
<path value="$PROJECT_DIR$/vendor/phpoption/phpoption" />
<path value="$PROJECT_DIR$/vendor/graham-campbell/result-type" />
<path value="$PROJECT_DIR$/vendor/vlucas/phpdotenv" />
<path value="$PROJECT_DIR$/vendor/symfony/polyfill-php83" />
</include_path>
</component>
<component name="PhpInterpreters">
+5
View File
@@ -32,5 +32,10 @@
"require-dev": {
"vimeo/psalm": "^4.27",
"phpunit/phpunit": "^9.5"
},
"config": {
"allow-plugins": {
"php-http/discovery": true
}
}
}
Generated
+906 -1105
View File
File diff suppressed because it is too large. Load diff
@@ -63,7 +63,11 @@
{
/** @var DataBase $DB Объект БД */
$DB = DataBase::getInstance();
$res = $DB->query('SELECT * FROM `' . self::TABLE . '` ORDER BY `id` ' . $sort . ' LIMIT ' . $limit);
// ORDER BY/LIMIT не подставляются через плейсхолдер: направление сортировки идёт через
// белый список, лимит приводится к int.
$safeSort = strtoupper($sort) === 'DESC' ? 'DESC' : 'ASC';
$safeLimit = implode(', ', array_map('intval', explode(',', $limit)));
$res = $DB->query('SELECT * FROM `' . self::TABLE . '` ORDER BY `id` ' . $safeSort . ' LIMIT ' . $safeLimit);
return $res ?? [];
}
@@ -82,7 +82,7 @@
{
/** @var DataBase $db */
$DB = DataBase::getInstance();
$DB->query('DELETE FROM ' . self::TABLE . ' WHERE last_active < ' . (time() - $this->timeInterval));
$DB->query('DELETE FROM ' . self::TABLE . ' WHERE last_active < :threshold', ['threshold' => time() - $this->timeInterval]);
}
public function checkDDos(): void
+6 -6
View File
@@ -76,7 +76,7 @@
$text = htmlspecialchars_decode($text);
$text = str_replace('\'', '\\\'', $text);
$hash = md5($text);
$res = $DB->query('SELECT * FROM bashorg WHERE hash="' . $hash . '"');
$res = $DB->query('SELECT * FROM bashorg WHERE hash=:hash', ['hash' => $hash]);
if (!$res) {
$i++;
$DB->add('bashorg', ['hash' => $hash, 'text' => $text]);
@@ -105,7 +105,7 @@
$text = htmlspecialchars_decode($text);
$text = str_replace('\'', '\\\'', $text);
$hash = md5($text);
$res = $DB->query('SELECT * FROM bashorg WHERE hash="' . $hash . '"');
$res = $DB->query('SELECT * FROM bashorg WHERE hash=:hash', ['hash' => $hash]);
if (!$res) {
$i++;
$itemId = $DB->add('bashorg', ['hash' => $hash, 'text' => $text]);
@@ -166,7 +166,7 @@
$text = htmlspecialchars_decode($text);
$text = str_replace('\'', '\\\'', $text);
$hash = md5($text);
$res = $DB->query('SELECT * FROM bashorg WHERE hash="' . $hash . '" AND date is null');
$res = $DB->query('SELECT * FROM bashorg WHERE hash=:hash AND date is null', ['hash' => $hash]);
if ($res) {
$j++;
$DB->update(
@@ -197,7 +197,7 @@
$hash = md5($joke);
$joke = str_replace('<br>', PHP_EOL, $joke);
$joke = addslashes($joke);
$res = $DB->query('SELECT * FROM jokes WHERE hash="' . $hash . '"');
$res = $DB->query('SELECT * FROM jokes WHERE hash=:hash', ['hash' => $hash]);
if (!$res) {
$i++;
$itemId = $DB->add('jokes', ['hash' => $hash, 'text' => $joke]);
@@ -245,7 +245,7 @@
$hash = md5($file);
$file = addslashes($file);
$res = $DB->query('SELECT * FROM jokes WHERE hash="' . $hash . '"');
$res = $DB->query('SELECT * FROM jokes WHERE hash=:hash', ['hash' => $hash]);
if (!$res) {
$itemId = $DB->add('jokes', ['hash' => $hash, 'text' => $file]);
$result = 'Добавлен новый элемент с ID ' . $itemId;
@@ -310,7 +310,7 @@ DELETE from `jokes` WHERE `jokes`.id not in (SELECT id FROM t_temp);'
$link = 'https://myslo.ru' . trim($link[1][0]);
$hash = md5($link);
$res = $DB->query('SELECT * FROM myslo WHERE hash="' . $hash . '"');
$res = $DB->query('SELECT * FROM myslo WHERE hash=:hash', ['hash' => $hash]);
if (!$res) {
$full = file_get_contents($link);
+17 -7
View File
@@ -47,8 +47,8 @@
$DB = DataBase::getInstance();
$dialogId = $DB->query(
'SELECT `id` FROM ' . self::TABLE_DIALOGS
. ' WHERE (`send`="' . $userOne . '" and `receive`="' . $userTwo
. '") OR (`send`="' . $userTwo . '" and `receive`="' . $userOne . '")'
. ' WHERE (`send`=:userOne1 and `receive`=:userTwo1) OR (`send`=:userTwo2 and `receive`=:userOne2)',
['userOne1' => $userOne, 'userTwo1' => $userTwo, 'userTwo2' => $userTwo, 'userOne2' => $userOne]
);
if (!$dialogId) {
return null;
@@ -87,7 +87,8 @@
/** @var $DB DataBase Объект базы данных */
$DB = DataBase::getInstance();
$dialogs = $DB->query(
'SELECT * FROM ' . self::TABLE_DIALOGS . ' WHERE `send`="' . $this->user->getId() . '" OR `receive`="' . $this->user->getId() . '" ORDER BY `date_updated` DESC'
'SELECT * FROM ' . self::TABLE_DIALOGS . ' WHERE `send`=:userId1 OR `receive`=:userId2 ORDER BY `date_updated` DESC',
['userId1' => $this->user->getId(), 'userId2' => $this->user->getId()]
);
if (empty($dialogs)) {
return [];
@@ -107,7 +108,7 @@
{
/** @var $DB DataBase Объект базы данных */
$DB = DataBase::getInstance();
$dialog = $DB->query('SELECT * FROM ' . self::TABLE_DIALOGS . ' WHERE `id`="' . $dialogId . '"');
$dialog = $DB->query('SELECT * FROM ' . self::TABLE_DIALOGS . ' WHERE `id`=:dialogId', ['dialogId' => $dialogId]);
if (empty($dialog)) {
return null;
}
@@ -136,9 +137,15 @@
$DB = DataBase::getInstance();
$paginationSqlString = '';
if ($paginationData !== null) {
$paginationSqlString = ' LIMIT ' . $paginationData['from'] . ', ' . $paginationData['to'];
// LIMIT/OFFSET не могут быть переданы через именованный плейсхолдер подготовленного
// выражения (PDO::ATTR_EMULATE_PREPARES выключен для MySQL), поэтому приводим к int и
// подставляем в текст запроса напрямую — приведение типа исключает инъекцию.
$paginationSqlString = ' LIMIT ' . (int)$paginationData['from'] . ', ' . (int)$paginationData['to'];
}
$messages = $DB->query('SELECT * FROM ' . self::TABLE_MESSAGES . ' WHERE `dialog_id`="' . $dialogId . '" ORDER BY id ASC' . $paginationSqlString);
$messages = $DB->query(
'SELECT * FROM ' . self::TABLE_MESSAGES . ' WHERE `dialog_id`=:dialogId ORDER BY id ASC' . $paginationSqlString,
['dialogId' => $dialogId]
);
if ($markDialogViewed) {
$this->markDialogViewed($dialogId);
}
@@ -250,7 +257,10 @@
{
/** @var $DB DataBase Объект базы данных */
$DB = DataBase::getInstance();
$res = $DB->query('SELECT * FROM ' . self::TABLE_MESSAGES . ' WHERE `dialog_id`="' . $dialogId . '" ORDER BY id DESC LIMIT 1');
$res = $DB->query(
'SELECT * FROM ' . self::TABLE_MESSAGES . ' WHERE `dialog_id`=:dialogId ORDER BY id DESC LIMIT 1',
['dialogId' => $dialogId]
);
return $res ? $res[0] : null;
}
+2 -2
View File
@@ -84,7 +84,7 @@
*/
private function loadFileProps(): self
{
$res = $this->DB->query('SELECT * FROM ' . self::TABLE . ' WHERE id="' . $this->id . '"')[0];
$res = $this->DB->query('SELECT * FROM ' . self::TABLE . ' WHERE id=:id', ['id' => $this->id])[0];
if (empty($res)) {
throw new CoreException('Файл с идентификатором ' . $this->id . ' не найден', CoreException::ERROR_FILE_NOT_FOUND);
}
@@ -159,7 +159,7 @@
*/
public function getAllProps(): ?array
{
return $this->DB->query('SELECT * FROM ' . self::TABLE . ' WHERE id="' . $this->id . '"')[0];
return $this->DB->query('SELECT * FROM ' . self::TABLE . ' WHERE id=:id', ['id' => $this->id])[0];
}
+31 -14
View File
@@ -184,7 +184,8 @@
private function getActiveTasksCount(): int
{
return (int)$this->DB->query(
'SELECT count(id) AS count FROM ' . self::TABLE . ' WHERE active="' . self::VALUE_Y . '"'
'SELECT count(id) AS count FROM ' . self::TABLE . ' WHERE active=:active',
['active' => self::VALUE_Y]
)[0]['count'];
}
@@ -220,13 +221,14 @@
// Вычисляем сколько заданий требуется докинуть
$num = self::EXECUTION_TASKS_LIMIT - $countTasks;
$arTasks = $this->DB->query(
'SELECT id FROM ' . self::TABLE . ' WHERE active="' . self::VALUE_N . '" AND in_progress="' . self::VALUE_N
. '" ORDER BY priority ASC LIMIT ' . $num
'SELECT id FROM ' . self::TABLE . ' WHERE active=:active AND in_progress=:inProgress'
. ' ORDER BY priority ASC LIMIT ' . (int)$num,
['active' => self::VALUE_N, 'inProgress' => self::VALUE_N]
);
if (!empty($arTasks)) {
$arTaskIds = [];
foreach ($arTasks as $task) {
$arTaskIds[] = $task['id'];
$arTaskIds[] = (int)$task['id'];
}
Log::logToFile(
$this->getWorkerId() . ': Взято в работу заданий ' . count($arTaskIds),
@@ -237,7 +239,10 @@
false
);
$this->DB->query('UPDATE ' . self::TABLE . ' SET active="' . self::VALUE_Y . '" WHERE id IN (' . implode(',', $arTaskIds) . ')');
$this->DB->query(
'UPDATE ' . self::TABLE . ' SET active=:active WHERE id IN (' . implode(',', $arTaskIds) . ')',
['active' => self::VALUE_Y]
);
}
}
}
@@ -488,8 +493,8 @@
private function checkDuplicates(?string $class, string $method, string $params): bool
{
$count = $this->DB->query(
'SELECT count(id) as count FROM ' . self::TABLE . ' WHERE class="' . addslashes($class) . '" and method="' . $method
. '" and params="' . addslashes($params) . '"'
'SELECT count(id) as count FROM ' . self::TABLE . ' WHERE class=:class and method=:method and params=:params',
['class' => (string)$class, 'method' => $method, 'params' => $params]
)[0]['count'];
return ($count > 0);
}
@@ -694,7 +699,13 @@
*/
public function getTasks(string $limit = '10', string $orderBy = 'id', string $sort = 'DESC'): ?array
{
return $this->DB->query('SELECT * FROM ' . self::TABLE . ' ORDER BY ' . $orderBy . ' ' . $sort . ' LIMIT ' . $limit) ?? [];
// ORDER BY/LIMIT — синтаксис запроса, а не значения, поэтому не подставляются через
// плейсхолдер: имя колонки идёт через белый список символов, направление сортировки —
// через белый список ASC/DESC, лимит приводится к int.
$safeOrderBy = preg_replace('/[^a-zA-Z0-9_]/', '', $orderBy);
$safeSort = strtoupper($sort) === 'ASC' ? 'ASC' : 'DESC';
$safeLimit = implode(', ', array_map('intval', explode(',', $limit)));
return $this->DB->query('SELECT * FROM ' . self::TABLE . ' ORDER BY `' . $safeOrderBy . '` ' . $safeSort . ' LIMIT ' . $safeLimit) ?? [];
}
/**
@@ -707,7 +718,11 @@
*/
public function getTasksHistory(string $limit = '10', string $orderBy = 'id', string $sort = 'DESC'): ?array
{
return $this->DB->query('SELECT * FROM ' . self::TABLE_HISTORY . ' ORDER BY ' . $orderBy . ' ' . $sort . ' LIMIT ' . $limit) ?? [];
// См. комментарий в getTasks() — ORDER BY/LIMIT идут через белый список/приведение типа.
$safeOrderBy = preg_replace('/[^a-zA-Z0-9_]/', '', $orderBy);
$safeSort = strtoupper($sort) === 'ASC' ? 'ASC' : 'DESC';
$safeLimit = implode(', ', array_map('intval', explode(',', $limit)));
return $this->DB->query('SELECT * FROM ' . self::TABLE_HISTORY . ' ORDER BY `' . $safeOrderBy . '` ' . $safeSort . ' LIMIT ' . $safeLimit) ?? [];
}
/**
@@ -791,7 +806,8 @@
{
$arStuckTasks = [];
$runTasks = $this->DB->query(
'SELECT * FROM ' . self::TABLE . ' WHERE active="' . self::VALUE_Y . '" AND in_progress="' . self::VALUE_Y . '"'
'SELECT * FROM ' . self::TABLE . ' WHERE active=:active AND in_progress=:inProgress',
['active' => self::VALUE_Y, 'inProgress' => self::VALUE_Y]
);
if (!empty($runTasks)) {
foreach ($runTasks as $task) {
@@ -812,10 +828,11 @@
if (!empty($arStuckTasks)) {
$this->DB->query(
'UPDATE ' . self::TABLE . ' SET active="' . self::VALUE_N . '", in_progress="' . self::VALUE_N . '" WHERE id in(' . implode(
'UPDATE ' . self::TABLE . ' SET active=:active, in_progress=:inProgress WHERE id in(' . implode(
',',
$arStuckTasks
) . ')'
) . ')',
['active' => self::VALUE_N, 'inProgress' => self::VALUE_N]
);
sendTelegram('Задания были возвращены в работу (' . count($arStuckTasks) . ' шт)' . PHP_EOL . implode(', ', $arStuckTasks));
@@ -841,9 +858,9 @@
*/
public function getTaskResponse(int $taskId): MQResponse
{
$res = $this->DB->query('SELECT * FROM ' . self::TABLE . ' WHERE id="' . $taskId . '"')[0];
$res = $this->DB->query('SELECT * FROM ' . self::TABLE . ' WHERE id=:id', ['id' => $taskId])[0];
if (empty($res)) {
$res = $this->DB->query('SELECT * FROM ' . self::TABLE_HISTORY . ' WHERE task_id="' . $taskId . '"')[0];
$res = $this->DB->query('SELECT * FROM ' . self::TABLE_HISTORY . ' WHERE task_id=:taskId', ['taskId' => $taskId])[0];
}
if (empty($res)) {
throw new CoreException('Задание с ID ' . $taskId . ' не найдено');
+16 -7
View File
@@ -75,13 +75,20 @@
/** @var $DB DataBase */
$DB = DataBase::getInstance();
$sql = 'SELECT * FROM ' . self::TABLE;
$sql = 'SELECT * FROM ' . self::TABLE;
$params = [];
if (!empty($this->sectionId)) {
$sql .= ' WHERE section_id="' . $this->sectionId . '"';
$sql .= ' WHERE section_id=:sectionId';
$params['sectionId'] = $this->sectionId;
}
$sql .= ' ORDER BY id ' . $sort . ' LIMIT ' . $limit;
return $DB->query($sql) ?: [];
// ORDER BY/LIMIT не являются значениями и не подставляются через плейсхолдер:
// направление сортировки идёт через белый список, лимит (может быть строкой
// вида "0, 10") разбирается на числа и приводится к int.
$safeSort = strtoupper($sort) === 'ASC' ? 'ASC' : 'DESC';
$safeLimit = implode(', ', array_map('intval', explode(',', $limit)));
$sql .= ' ORDER BY id ' . $safeSort . ' LIMIT ' . $safeLimit;
return $DB->query($sql, $params) ?: [];
}
public function getAllPostData(): ?array
@@ -115,11 +122,13 @@
/** @var $DB DataBase */
$DB = DataBase::getInstance();
$sql = 'SELECT count(*) as count FROM ' . self::TABLE;
$sql = 'SELECT count(*) as count FROM ' . self::TABLE;
$params = [];
if (!empty($this->sectionId)) {
$sql .= ' WHERE section_id = "' . $this->sectionId . '"';
$sql .= ' WHERE section_id=:sectionId';
$params['sectionId'] = $this->sectionId;
}
$res = $DB->query($sql);
$res = $DB->query($sql, $params);
return $res ? (int)$res[0]['count'] : 0;
}
+7 -3
View File
@@ -92,7 +92,7 @@
/** @var $DB DataBase */
$DB = DataBase::getInstance();
$res = $DB->query('SELECT role_id FROM `' . self::USER_ROLES_TABLE . '` WHERE user_id=' . $this->user->getId());
$res = $DB->query('SELECT role_id FROM `' . self::USER_ROLES_TABLE . '` WHERE user_id=:userId', ['userId' => $this->user->getId()]);
$userRoles = [];
if (!empty($res)) {
foreach ($res as $row) {
@@ -137,7 +137,10 @@
/** @var $DB DataBase */
$DB = DataBase::getInstance();
return $DB->query('DELETE FROM ' . self::USER_ROLES_TABLE . ' WHERE user_id=' . $this->user->getId() . ' AND role_id=' . $roleId);
return $DB->query(
'DELETE FROM ' . self::USER_ROLES_TABLE . ' WHERE user_id=:userId AND role_id=:roleId',
['userId' => $this->user->getId(), 'roleId' => $roleId]
);
}
/**
@@ -153,7 +156,8 @@
return $DB->query(
'SELECT ' . self::ROLES_TABLE . '.id, ' . self::ROLES_TABLE . '.name, ' . self::ROLES_TABLE . '.description FROM '
. self::USER_ROLES_TABLE . ' left JOIN ' . self::ROLES_TABLE . ' ON ' . self::USER_ROLES_TABLE . '.role_id = ' . self::ROLES_TABLE
. '.id WHERE ' . self::USER_ROLES_TABLE . '.user_id = "' . $this->user->getId() . '"'
. '.id WHERE ' . self::USER_ROLES_TABLE . '.user_id = :userId',
['userId' => $this->user->getId()]
) ?: [];
}
+18 -7
View File
@@ -244,11 +244,17 @@
*/
public static function getUsers(string $limit = '10', string $sort = 'ASC'): array
{
$cacheId = md5('User_getUsers_' . $limit . '_' . $sort);
// ORDER BY/LIMIT — это не значения, а часть синтаксиса запроса, поэтому плейсхолдер
// подготовленного выражения тут не применим: направление сортировки идёт через
// белый список, а лимит (может быть строкой вида "0, 10" от Pagination::getLimit())
// разбирается на числа и приводится к int перед подстановкой.
$safeSort = strtoupper($sort) === 'DESC' ? 'DESC' : 'ASC';
$safeLimit = implode(', ', array_map('intval', explode(',', $limit)));
$cacheId = md5('User_getUsers_' . $safeLimit . '_' . $safeSort);
if (Cache::check($cacheId)) {
$res = Cache::get($cacheId);
} else {
$res = (DataBase::getInstance())->query('SELECT * FROM `'.self::TABLE.'` ORDER BY `id` '.$sort.' LIMIT '.$limit);
$res = (DataBase::getInstance())->query('SELECT * FROM `'.self::TABLE.'` ORDER BY `id` '.$safeSort.' LIMIT '.$safeLimit);
Cache::set($cacheId, $res);
}
return $res;
@@ -390,7 +396,7 @@
*/
public static function isOnline(int $id): bool
{
$res = (DataBase::getInstance())->query('SELECT last_active FROM `'.self::TABLE.'` WHERE id='.$id);
$res = (DataBase::getInstance())->query('SELECT last_active FROM `'.self::TABLE.'` WHERE id=:id', ['id' => $id]);
$last_active = $res[0]['last_active'];
$timeNow = time();
if ($last_active > ($timeNow - USER_ONLINE_TIME)) {
@@ -407,7 +413,10 @@
*/
public static function getOnlineCount(): int
{
$res = (DataBase::getInstance())->query('SELECT COUNT(*) as count FROM `' . self::TABLE . '` WHERE last_active > ' . (time() - USER_ONLINE_TIME));
$res = (DataBase::getInstance())->query(
'SELECT COUNT(*) as count FROM `' . self::TABLE . '` WHERE last_active > :threshold',
['threshold' => time() - USER_ONLINE_TIME]
);
return (int)$res[0]['count'];
}
@@ -974,11 +983,13 @@
{
/** @var $DB DataBase Объект базы данных */
$DB = DataBase::getInstance();
$sql = 'SELECT COUNT(*) as count FROM ' . self::TABLE;
$sql = 'SELECT COUNT(*) as count FROM ' . self::TABLE;
$params = [];
if ($onlyConfirmed) {
$sql .= ' WHERE email_confirmed="' . CODE_VALUE_Y . '"';
$sql .= ' WHERE email_confirmed=:emailConfirmed';
$params['emailConfirmed'] = CODE_VALUE_Y;
}
$res = $DB->query($sql);
$res = $DB->query($sql, $params);
return (int)$res[0]['count'];
}
+9 -3
View File
@@ -38,7 +38,7 @@
/** @var $DB DataBase */
$DB = DataBase::getInstance();
$res = $DB->query('SELECT * FROM `' . self::TABLE . '` WHERE user_id=' . $this->user->getId());
$res = $DB->query('SELECT * FROM `' . self::TABLE . '` WHERE user_id=:userId', ['userId' => $this->user->getId()]);
$userMeta = [];
if (!empty($res)) {
foreach ($res as $meta) {
@@ -70,7 +70,10 @@
} else {
/** @var $DB DataBase */
$DB = DataBase::getInstance();
$res = $DB->query('SELECT * FROM `' . self::TABLE . '` WHERE user_id=' . $this->user->getId() . ' AND name="' . $name . '"');
$res = $DB->query(
'SELECT * FROM `' . self::TABLE . '` WHERE user_id=:userId AND name=:name',
['userId' => $this->user->getId(), 'name' => $name]
);
if (!empty($res)) {
$userMeta = [];
foreach ($res as $meta) {
@@ -122,7 +125,10 @@
/** @var $DB DataBase */
$DB = DataBase::getInstance();
$DB->query('DELETE FROM ' . self::TABLE . ' WHERE user_id=' . $this->user->getId() . ' AND id="' . $id . '"');
$DB->query(
'DELETE FROM ' . self::TABLE . ' WHERE user_id=:userId AND id=:id',
['userId' => $this->user->getId(), 'id' => $id]
);
}
/**