Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php PDO查询变量问题?_Php_Pdo_Mysqli - Fatal编程技术网

Php PDO查询变量问题?

Php PDO查询变量问题?,php,pdo,mysqli,Php,Pdo,Mysqli,在下面的查询中,我使用entry\u id值从exp\u关系中查找exp\u channel\u数据中的最小值(一年)。我会给你一些虚拟数据 条目id 1的年份为2014年 条目id 2的年份为2012年 第一个查询生成“1,2”,我保存它以供下一个查询提供WHERE参数。如果我输入“1,2”而不是:showid,它将正常工作并显示2012,因为这是我设置的顺序中的第一个。但是,当查询按照下面所写的方式运行时,它会显示2014 $sql = 'SELECT DISTINCT parent_id

在下面的查询中,我使用entry\u id值从exp\u关系中查找exp\u channel\u数据中的最小值(一年)。我会给你一些虚拟数据

条目id 1的年份为2014年 条目id 2的年份为2012年

第一个查询生成“1,2”,我保存它以供下一个查询提供WHERE参数。如果我输入“1,2”而不是:showid,它将正常工作并显示2012,因为这是我设置的顺序中的第一个。但是,当查询按照下面所写的方式运行时,它会显示2014

$sql = 'SELECT DISTINCT parent_id FROM exp_relationships WHERE child_id = :entry_id AND grid_field_id IN (90, 91)';
$stmt = $conn->prepare($sql);
$showid = "";

try {
    $stmt->execute(array('entry_id' => {entry_id}));
    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
    foreach ($result as $row) {
        $showid .= $row['parent_id'].", ";
    }
    $showid = substr($showid, 0, -2);
} catch(PDOException $e) { error_log($e->getMessage(),0); }

$sql = 'SELECT field_id_16 FROM exp_channel_data WHERE entry_id IN (:showid) ORDER BY field_id_16 ASC LIMIT 1';
$stmt = $conn->prepare($sql);
$year = "";

try {
    $stmt->execute(array('showid' => $showid));
    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
    foreach ($result as $row) {
        $year = $row['field_id_16'];
        echo $year;
    }
} catch(PDOException $e) { error_log($e->getMessage(),0); }

这是因为默认情况下,它会将$showid转换为其基本类型string或int。在您的例子中,我猜if将是字符串
1,2
。这样,您就不能只在中的
中注入变量值来模拟多个值。每个占位符都是一个值,(:placeholder)
中的
将只引用一个值。您需要根据需要拆分尽可能多的占位符,即在本例中,在(?)
中拆分两个占位符
,然后将
$showid
字符串内爆为数组。或者将<代码> $SuffID <代码>放入数组,然后将其拆分为<代码> > 子句,我认为两者都是更坏的方式。