PDO bindValue problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
spamyboy
Forum Contributor
Posts: 266
Joined: Sun Nov 06, 2005 11:29 am
Location: Lithuania, vilnius

PDO bindValue problem

Post by spamyboy »

Code: Select all

function update_user($user_id, $array) {
        $result = $this->db->prepare("UPDATE `gcms_user`
                                     SET ?=?
                                     WHERE `user_id`=?;");
            $result->bindValue(3,   $user_id,   PDO::PARAM_INT);
        foreach ($array as $key=>$value) {
            $result->bindValue(1,   $key,       PDO::PARAM_STR); // Problem here
            $result->bindValue(2,   $value,     PDO::PARAM_STR);
            $result->execute();
        }
    }
Problem is basic. Query is generated like "UPDATE `gcms_user` SET 'key_or_1_var'='value_or_2_var' WHERE `user_id`='#id#';"
So basically key_or_1_var is taken as variable since it is with '' and how to change that to ``?
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Re: PDO bindValue problem

Post by infolock »

I'd be interested to see the array you are passing to it first of all.

Secondly, I'd also be interested in seeing the SQL that is generated from this to see what it looks like as well.
Post Reply