Sql 运算符 "and" 和 "or" 无法正常工作

Sql operator "and" and "or" not working correctly

当我 运行 这段代码时,布尔 andor 操作似乎有问题:

$ne = News::find(['conditions' => 'title_md = "' . htmlspecialchars($_post['title_md']) . '"  OR alias = "'.$item->alias.'" AND id !=  ' . $id])->toArray();

if(count($ne) < 1)returnstrue里面的条件,但是我需要获取false因为id目前没有被占用

这是充分利用 Phalcon 的 ORM 潜力的正确查询。

$ne = News::find([
    'conditions' => '(title_md = :title: OR alias = :alias:) AND id != :id:',
    'bind' => [
        'title' => $_POST['title_md'],
        'alias' => $item->alias,
        'id' => $id,
    ]
])->toArray();

正如上面提到的,您在绑定参数时必须更加小心,以避免 SQL 注入。文档中的更多示例和操作方法:https://docs.phalconphp.com/zh/3.2/db-models#binding-parameters