I have a pdo command like this $sql = $pdoObj->execute($query) but it does not work, 0 result returning. I echo'ed out the $query variable just before calling execute() and then pasted it inside the execute() and the code ran successfully. I can't understand what is the problem here as I have done this in other parts of my code without problems. Here are some examples of the queries:

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')

They are all dynamic generated queries based on the $_GET variable.

EDIT: Sure

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 ";
            }

EDIT2:

I use query() and not execute() but still does not work

有帮助吗?

解决方案

The arguments for execute should be an array with query parameters. You mean to use either

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

OR

$stmt = $pdoObj->prepare($query);
$stmt->execute();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top