noncent / pdo_class_wrapper

A Class for PDO Wrapper
25 stars 21 forks source link

Update query is not executing #6

Closed narasimha-kvl closed 9 years ago

narasimha-kvl commented 9 years ago

Hi

I am using you pdo wrapper. I am having problem in updating sql query. I am not getting any error but the query is not updating

$pObjDB->update('usertable', array('username' => $newuser, 'password' => $passwordMD5, 'acctype' => 0), array('username' => $username), 'LIMIT 1')->affectedRows();

I am not able to understand what I am doing wrong, but the same query is running successfully in command prompt.

UPDATE usertable SET username = "test12", password = "81dc9bdb52d04dc20036dbd8313ed055", acctype = 0 WHERE username = "test" LIMIT 1;

Could please help me out

noncent commented 9 years ago

Hi narasimha-kvl,

Thanks for using PDO_Class_Wrapper,

The problem is that you are using 'username' field twice in your query and this is the reason PDO Wrapper creating final query like this:

UPDATE `usertable` SET password = :s_password, acctype = :s_acctype, username = :s_username WHERE username = :s_username LIMIT 1;

The PDO Wrapper use namespace bind param to keep safe query's and in your query PDO Wrapper getting two same field and that's why your 'username' filed not updating or it's remains same as old.

While you executing raw query it's working but not with PDO Wrapper.

UPDATE usertable SET username = "test12", password = "81dc9bdb52d04dc20036dbd8313ed055", acctype = 0 WHERE username = "test" LIMIT 1;

The good practice is that always use primary key to update tables records. like below

$pObjDB->update('usertable', array('username' => $newuser, 'password' => $passwordMD5, 'acctype' => 0), array('id' => $uid))->affectedRows();

else you can user 'pdoQuery' method:

$pObjDB->pdoQuery("UPDATE usertable SET username = ?, password = ?, acctype = ? WHERE username = ? LIMIT 1;",array("test12","81dc9bdb52d04dc20036dbd8313ed055",'0',"test"))->showQuery()->affectedRows();

Sorry for inconvenience :( I will update this bug on next release. I am working on next release and it would be here soon and it would be perfect!! and more reliable :)

Cheers!!!! :+1:

narasimha-kvl commented 9 years ago

Thank you for quick help. I had a special scenario for change in username and that table doesn't had a primary key