PDO fetch returns nothing [duplicate]

你说的曾经没有我的故事 提交于 2019-12-30 23:35:22

问题


I've encountered 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);

Which returns:

array(0) { } 

I've tried the query in phpmyadmin and it worked fine. Can you please help me find the error?


回答1:


Accorrding to the documentation

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

In you 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'] 
                         )
                   );  


来源:https://stackoverflow.com/questions/15192553/pdo-fetch-returns-nothing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!