dongpo1846 2012-04-18 10:29
浏览 25
已采纳

PHP PDO访问问题

I currently have a database call which works in one area, but not another.

My database class is labeled $database and has the $db PDO object passed to the database constructor.

This all works fine.

This code call works

$user_info = $database->get_user($user);

This function is called

public function get_user($user){
        $user = $this->db->quote($user);
        $query = "SELECT * FROM login where username = $user";
        $user = $this->db->query($query);
        $user_info = $user->fetch();
        return $user_info;}

This returns the user info as expected

Another call is

        $user = $database->confirm($confirm);
        $user_info = $database->get_user($user);

The confirm function looks like this

        public function confirm($confirm){
        $confirm = $this->db->quote($confirm);
        $query = "SELECT * FROM temp_data WHERE hash = $confirm;";
        $count = $this->db->query($query);
        $info = $count->fetch();

        $user = $info['username'];
        $user = $this->db->quote($user);
        $query = "DELETE FROM temp_data WHERE username = $user;";
        $this->db->exec($query);
        $query = "UPDATE login SET confirm = '1' WHERE  username = $user;";
        $this->db->exec($query);
        return $info['username'];}

This call works perfectly.

It returns the username in an unquoted format which the get_user accepts

However the get_user fails

I have done troubleshooting and found the issue is that the following line in get_user is not returning any PDO object, or any data at all

$user = $this->db->query($query); 

This is the interesting part. I did a try catch and the PDO is throwing no PDO exception and it is not even throwing a general exception. There are no errors in the error log. It just appears not to be working. I have went through step by step using Print_r, get_class, and echo followed by a die; to output my results and it all works up until I do the second database call. The fetch is returning an error, but that is only due to the fact the exec is not running at all it seems

I did try using unset() on everything but my main DB connection that is created and passed into my database object when it is created

Here is what is happening when the script is run

  1. The user clicks on a link with a random hash
  2. The system gets the hash from the GET and then uses that too pull the user name from the temp database.
  3. It then modifies the user as confirmed, deletes the temp entry and then returns the user name
  4. from here it then calls the get_user. In this case it just returns the user information to set up the twilio sub account. This is where the system is failing
  5. After that what happens (And this it does try to do from my testing because i get an error here) is that it accesses the twilio API, gets a sub acount AUTH and SID token and writes it to the users file
  6. Then the new user is redirected to the signin page

I have tried using close cursor and it has not worked.

If anyone can give me any insight into the issue I am having, or what I am doing wrong I would appreciate it. I am new to using the PDO object so I am sure I have made a stupid mistake somewhere.

展开全部

  • 写回答

1条回答 默认 最新

  • drwj4061 2012-05-09 05:50
    关注

    I was able to get the answer from a great guy at the PHPFreaks IRC channel.

    public function get_user($user){
            $stmt = $this->db->prepare('SELECT * FROM login where username = :u');
            $stmt->bindValue(':u', $user);
            $stmt->execute();
    
            $user_info = $stmt->fetch();
            $stmt->closeCursor();
    
            return $user_info;
    }
    
    
    public function confirm($confirm){
            $stmt = $this->db->prepare('SELECT * FROM temp_data WHERE hash = :h');
            $stmt->bindValue(':h', $confirm);
            $stmt->execute();
            $info = $stmt->fetch();
            $stmt->closeCursor();
    
            $stmt = $this->db->prepare('DELETE FROM temp_data WHERE username = :u');
            $stmt->bindValue(':u', $info['username']);
            $stmt->execute();
    
            $stmt = $this->db->prepare('UPDATE login SET confirm = '1' WHERE  username = :u');
            $stmt->bindValue(':u', $info['username']);
            $stmt->execute();
    
            return $info['username'];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部