php - 在不使用引号的情况下在查询中传递字符串 ''

标签 php mysql cakephp cakephp-1.2

我有这个查询代码:

$conditions[]=array('codiceBiblio IN (?)'=> $tot);

其中 $tot 是一个字符串(例如:2345,5657,4565,5678)。
在这种情况下,查询将是:

SELECT [...] WHERE codiceBiblio IN ('2345,5657,4565,5678')

但它只会返回第一条记录。

所以应该是:

SELECT [...] WHERE codiceBiblio IN (2345,5657,4565,5678)

我该怎么做?


如何构建查询

我有这个查询代码:

// General Query
$conditions = array(
    'editore LIKE' => "%$e%",
    'titolo LIKE' => "%$t%"
);

然后我用用户的选择填充 $conditions,例如:

if ($anno&&$anno2)
    $conditions[] = array('anno BETWEEN ? AND ?' => array($anno,$anno2));

if (isset($menu)&&$menu!='')
    $conditions[]=array('classe LIKE' => "%$menu%");

最佳答案

只需使用数组并省略 IN() 子句。手册 ( Complex Find Conditions ) 提供了这个例子:

array('Company.status' => array('inactive', 'suspended'))

... 生成以下 SQL:

`Company`.`status` IN ('inactive', 'suspended')

如果 $tot 是像 2345,5657,4565,5678 这样的字符串,您需要先将其 explode()

免责声明:这适用于 Cake 2,不确定 1.2。

关于php - 在不使用引号的情况下在查询中传递字符串 '',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15112779/

相关文章:

php - 如何使用 SendGrid 发送电子邮件

php - 在 SQL 命令中使用包含刻度线/引号的字符串

PHPUnit 和 die 函数

php - 特定邮政编码 : PHP mysql 附近的用户

Cakephp 3 : How to receive get request data?

php - 上传文件无效

MySQL INSERT 和 SELECT 优先顺序

mysql - CakePHP 3 - MySQL 'BIGINT' 字段在实体中未正确处理

php - CakePHP - 使用 php 变量加载 View 并在 ajax 中返回

mysql - SQL 查询在免费搜索中从另一个表中查找名称