【问题标题】:Array return null for different SQL query filter types不同 SQL 查询过滤器类型的数组返回 null
【发布时间】:2016-11-24 11:49:44
【问题描述】:

我想向你们寻求帮助,我一直在处理一个我难以解决的问题。

我有这两种 SQL:

SQL1: select id, description from table where description = '1234';
SQL2: select id, description from table where description = 'record-data';

上面的两个 SQL 在执行时都运行良好,下面是表中的数据类型设置:

id:integer, description:character varying(30)

现在,我在表中存储了记录,其中描述字段包含整数和字符串值。

问题来了,当向每个 SQL 发出 print_r 时,SQL1 为空白,而 SQL2 返回数组元素数据/结果

SQL1: Array()
SQL2: Array([0] => Array
(
        [id] => 9
        [0] => 9
        [description] => record-data
        [1] => record-data
)

这两个数据都存在于表中 - 我似乎无法理解我遇到的问题。在这种情况下需要您的想法和帮助。

顺便说一句,我使用 PostgreSQL 作为我的数据库。

谢谢!

这是我的 php 代码/函数:

private function getPrimIds($val){

$dbh = new PDO("pgsql:dbname=xxxx;host=x.x.x.x", xxx,xxx );

$sql = "select id, description from table where description = '$val'";
$exec = $dbh->prepare($sql);
$exec->execute();
$result = $exec->fetchAll();

print "<br>";
print_r($result);
die();

if(count($result) > 0){
    $pr_id = $result[0]["id"];
    }

    return $pr_id;
}

【问题讨论】:

  • 你能分享你的php代码吗?
  • @malutki5200 请参见上文,我已更新问题。谢谢!
  • 这意味着你没有描述'1234'。它可以是' 1234 ''1234 ' 或其他。所以再次检查你的表格数据
  • @u_mulder 我很确定它存在..没有空格
  • sql-engine 告诉你相反的情况。如果您不确定 php 代码 - 在控制台或 pgAdmin 中直接在 psql 上运行查询

标签: php sql arrays postgresql


【解决方案1】:

看来你的功能没问题。您要查找的值一定有问题。
尝试改变:

"select id, description from table where description = '$val'";


到:

"select id, description from table where description like '%$val%'";

【讨论】:

    猜你喜欢
    • 2021-05-29
    • 1970-01-01
    • 1970-01-01
    • 2012-01-08
    • 2017-08-26
    • 2017-10-17
    • 1970-01-01
    • 2021-08-16
    • 1970-01-01
    相关资源
    最近更新 更多