4

what wrong with this Query ??? i need this Query.

UPDATE  user_preferences SET user_preferences_value = '2'
 WHERE user_preferences_name = 'is_user_package_active'
 AND   user_id = '$user_id'
 AND   phone_service_id='$phone_service_id';

is above query is equal to ZF query below

 function Deactivate_Service($user_id,$phone_service_id){
           $DB = Zend_Db_Table_Abstract::getDefaultAdapter();
           $data = array('user_preferences_value' => 2);
           $where = "user_preferences_name = 'is_user_package_active' AND user_id = " . (int)$user_id ." AND phone_service_id = ".(int)$phone_service_id;
           $DB->update('user_preferences',$data, $where);
      }

i am getting 0 with my ZF Query

EDITED:

public function deactivateserviceAction(){
       $this->_helper->viewRenderer->setNeverRender();
       $user = new Zend_Session_Namespace('user');
       $user_id =$user->user_id;
       $phone_service_id      = $this->_getParam('phone_service_id');
       //$Deactive = new Account();
        $DB = Zend_Db_Table_Abstract::getDefaultAdapter();
 $DB->query("UPDATE  user_preferences SET user_preferences_value = '2'
  WHERE user_preferences_name = 'is_user_package_active' AND   user_id = '$user_id' AND phone_service_id='$phone_service_id'");
      // $a = $Deactive->Deactivate_Service($user_id,$phone_service_id);
      // var_dump($a);

 }
Edward Maya
  • 429
  • 2
  • 10
  • 25

1 Answers1

11

Your 'update' call seems to be fine, although in your case it is better to use the following syntax to build WHERE clause (but that's a stylistic thing):

$data = array('user_preference_value' => 2);

$where = array(
    'user_preferences_name = ?' => 'is_user_package_active',
    'user_id = ?' => $user_id,
    'phone_service_id = ?' => $phone_service_id
);

$DB->update('user_preferences', $data, $where);

So I suppose the problem here is with your default adapter. Are you sure you have set up the connection? Can you run SELECTs successfully with the same $DB object? Try running the plain SQL update with your object, i.e. $DB->query('Your raw UPDATE query here') to see if it works.

Also the standard way to obtain the default DB is from Zend_Db_Table, but that's also stylistic.

Yuriy
  • 1,964
  • 16
  • 23
  • my $DB object is working all over the project except in this case and i dont know why this is not working – Edward Maya Apr 03 '12 at 10:04
  • 1
    If you can't run SELECT's with it as well there is the only answer - your DB connection is not initialised properly in that branch of execution (basing on the fact it works elsewhere in your app). Impossible to tell where exactly without knowing your code, try to trace and debug to make sure you run the initialisation with proper credentials. – Yuriy Apr 03 '12 at 10:13
  • i try this $DB->query("UPDATE user_preferences SET user_preferences_value = '2' WHERE user_preferences_name = 'is_user_package_active' AND user_id = '$user_id' AND phone_service_id='$phone_service_id'"); – Edward Maya Apr 03 '12 at 10:36
  • but its not working too,but the class in which this function is i have another functions and there $DB is working how to debug ??? – Edward Maya Apr 03 '12 at 10:37
  • if $db is not initialized correct t this is not giving me the error.as a matter of fact no error t all and its not updating the record – Edward Maya Apr 03 '12 at 10:39
  • 1
    Sorry, but it is nearly impossible to tell what exactly is wrong without looking at the code. If you're sure $DB is initialised properly try calling it outside this function where before it's invoked, then again outside etc. to find the point at which it breaks. – Yuriy Apr 03 '12 at 10:58
  • when i do so echo $DB->update('user_preferences', $data, $where); its printing 0 – Edward Maya Apr 03 '12 at 11:16
  • 1
    That means your query succeeds but 0 rows match it. Which is legit when there are no rows in your table matching your WHERE condition. Make sure please there are rows matching that WHERE and try again. – Yuriy Apr 03 '12 at 13:22