不同SQL查询过滤器类型的数组返回null



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

我有这两种类型的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
)

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

btw,我正在使用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;
}

看来您的功能还可以。您要寻找的价值必须存在问题。
尝试更改:

"选择ID,从表中的描述中description ='$ val'';


to:

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

最新更新