PDO selection returns nothing

I ran into a little problem. I have the following code:

$query = $db->prepare('(SELECT last_visit, last_ip FROM user_log WHERE user_id = :id) UNION (SELECT time AS last_visit, packet_hex AS last_ip FROM crack_log WHERE target_id = :id) ORDER BY last_visit DESC LIMIT 0,10;'); $query->execute(array(':id'=> $_SESSION['id'])); $log = $query->fetchAll(PDO::FETCH_ASSOC); //Last visit/IP var_dump($log); 

What returns:

 array(0) { } 

I tried the request in phpmyadmin and it worked fine. Could you help me find a mistake?

+4
source share
1 answer

Documentation Confirmation

You cannot use the handle of a named parameter with the same name twice in a prepared expression. You cannot bind multiple values ​​to a single named parameter in, for example, the IN () clause of an SQL statement.

In this case you should use something like

 $query = $db->prepare('(SELECT last_visit, last_ip FROM user_log WHERE user_id = :id_1 ) UNION (SELECT time AS last_visit, packet_hex AS last_ip FROM crack_log WHERE target_id = :id_2 ) ORDER BY last_visit DESC LIMIT 0,10;' ); $query->execute(array(':id_1'=> $_SESSION['id'], ':id_2'=> $_SESSION['id'] ) ); 
+3
source

All Articles