-1

我有一个这样的 pdo 命令,$sql = $pdoObj->execute($query)但它不起作用,返回 0 结果。$query我在调用之前回显了变量execute(),然后将其粘贴execute()到代码中,代码运行成功。我无法理解这里的问题是什么,因为我在代码的其他部分中已经这样做了,没有问题。以下是一些查询示例:

SELECT s.id, s.marca, s.colore, s.prezzo, i.id_scarpa, i.taglia FROM scarpe AS s INNER JOIN info_scarpe AS i ON i.id_scarpa = s.id WHERE 1 = 1 AND taglia IN ('40','41','42') AND colore IN ('rosso', 'nero')

SELECT * FROM scarpe WHERE 1=1

SELECT * FROM scarpe WHERE 1=1 AND marca IN ('adidas','nike')

它们都是基于$_GET变量的动态生成查询。

编辑:当然

if ( isset($_GET) ) {

                if ( isset($_GET['taglia']) ) {

                    $query = "
                            SELECT 
                                s.id, s.marca, s.colore, s.prezzo, i.id_scarpa, i.taglia
                            FROM 
                                scarpe AS s
                            INNER JOIN
                                info_scarpe AS i
                            ON i.id_scarpa = s.id
                            WHERE
                                1 = 1
                    ";
                    foreach ( $_GET as $index => $val ) {
                        $a = explode(',', $val);
                        $in =  "'" . implode("','",  $a) . "'";
                        $query .= ' AND '.$index.' IN ('.$in.')';
                    }
                } else {
                    $query = " SELECT * FROM scarpe WHERE 1=1";
                    foreach ( $_GET as $index => $val ) {
                        $a = explode(',', $val);
                        $in =  "'" . implode("','",  $a) . "'";
                        $query .= ' AND '.$index.' IN ('.$in.')';
                    }
                }
                echo 'data loaded';
            } else {
                $query = " SELECT * FROM scarpe ORDER BY id DESC ";
            }

编辑2:

我使用query()而不是execute()但仍然无法正常工作

4

1 回答 1

0

的参数execute应该是一个带有查询参数的数组。你的意思是使用

$result = $pdoObj->query($query);

或者

$stmt = $pdoObj->prepare($query);
$stmt->execute();
于 2013-07-16T16:22:17.030 回答