Zend 查询在成功添加行时也不返回任何内容

发布于 2025-01-04 22:51:12 字数 1076 浏览 0 评论 0原文

我的 zend 查询有问题吗?这些查询扩展了 Zend_Db_Table_Abstract 类?

成功更新后更新将返回零 (0),它也应该是 1。添加新用户在成功插入时不会返回任何内容。

请帮我。

/*
* Adding new user into database
* @params unknown_type $title, $firstName, $lastName
* @return boolean $sql
*/
public function addNewUser( $title, $firstName, $lastName ) {
    $data = array (
         'user_id' => '',
         'title' => $title,
         'first_name' => $firstName,
         'last_name' => $lastName,
    );
    $result = $this->insert($data);
    return $result;
}

/*
* updating user details using given user id
* @params unknown_type values $userId, $title, $firstName, $lastName
* @return nuumber of rows update, hopefully 1
*/
public function updateUserDetails( $userId, $title, $firstName, $lastName )
{   //echo $userId;
    $data = array ( 'first_name' => $firstName, 'last_name' => $lastName );
    $where = $this  ->getAdapter()
                    ->quoteInto('user_id = ?', $userId);
    $result = $this ->update($data, $where );
    return $result; 
}

Is something wrong with my zend queries these are extending Zend_Db_Table_Abstract class?

Update is returning zero (0) on successful update also it should be one. Adding new user is return nothing on successful insert.

Please help me.

/*
* Adding new user into database
* @params unknown_type $title, $firstName, $lastName
* @return boolean $sql
*/
public function addNewUser( $title, $firstName, $lastName ) {
    $data = array (
         'user_id' => '',
         'title' => $title,
         'first_name' => $firstName,
         'last_name' => $lastName,
    );
    $result = $this->insert($data);
    return $result;
}

/*
* updating user details using given user id
* @params unknown_type values $userId, $title, $firstName, $lastName
* @return nuumber of rows update, hopefully 1
*/
public function updateUserDetails( $userId, $title, $firstName, $lastName )
{   //echo $userId;
    $data = array ( 'first_name' => $firstName, 'last_name' => $lastName );
    $where = $this  ->getAdapter()
                    ->quoteInto('user_id = ?', $userId);
    $result = $this ->update($data, $where );
    return $result; 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

睡美人的小仙女 2025-01-11 22:51:12

不要插入用户 ID。它可能是一个自动增量整数主键(如果不是,它应该是)。返回的值正是新插入行的主键值。

除此之外,代码看起来还不错。

Do not insert the user ID. It's probably an auto increment integer primary key (if not, it should be). The returned value is precisely the newly inserted row's primary key value.

That apart, the code seems OK.

咿呀咿呀哟 2025-01-11 22:51:12

以下是我对如何改进这些方法的意见

/*
* Adding new user into database
* @params unknown_type $title, $firstName, $lastName
* @return boolean $sql
*/
public function addNewUser( $title, $firstName, $lastName ) {
    $data = array (
         //'user_id' => '',  not needed fir insert if is primary key and auto incrementing
         'title' => $title,
         'first_name' => $firstName,
         'last_name' => $lastName,
    );
    $result = $this->insert($data);
    //Don't take chances, return know values. All you need to know at the controller is yes or no
    if (! $result) {
        return FALSE;  //or you can throw an exception
    } else {
        return TRUE;
    }
}

/*
* updating user details using given user id
* @params unknown_type values $userId, $title, $firstName, $lastName
* @return nuumber of rows update, hopefully 1
*/
public function updateUserDetails( $userId, $title, $firstName, $lastName )
{   //you forgot the title array key
    //if you are concerned there might be more then one row with this id test for it before the update.
    $rows = $this->fetchAll($userId);
    if ($rows->count() > 1) {
        throw new Exception()
     }        


    $data = array ( 'first_name' => $firstName, 'last_name' => $lastName, 'title' =>$title );
    $where = $this  ->getAdapter()
                    ->quoteInto('user_id = ?', $userId);
    $result = $this ->update($data, $where );
    //again ensure your method returns values that you want
    if (! $result) {
        return FALSE; //or exception or 0 or 'yankee doodle'
    } else {
    return TRUE; 
    }
}

The following is my opinion on how to improve these methods a little bit:

/*
* Adding new user into database
* @params unknown_type $title, $firstName, $lastName
* @return boolean $sql
*/
public function addNewUser( $title, $firstName, $lastName ) {
    $data = array (
         //'user_id' => '',  not needed fir insert if is primary key and auto incrementing
         'title' => $title,
         'first_name' => $firstName,
         'last_name' => $lastName,
    );
    $result = $this->insert($data);
    //Don't take chances, return know values. All you need to know at the controller is yes or no
    if (! $result) {
        return FALSE;  //or you can throw an exception
    } else {
        return TRUE;
    }
}

/*
* updating user details using given user id
* @params unknown_type values $userId, $title, $firstName, $lastName
* @return nuumber of rows update, hopefully 1
*/
public function updateUserDetails( $userId, $title, $firstName, $lastName )
{   //you forgot the title array key
    //if you are concerned there might be more then one row with this id test for it before the update.
    $rows = $this->fetchAll($userId);
    if ($rows->count() > 1) {
        throw new Exception()
     }        


    $data = array ( 'first_name' => $firstName, 'last_name' => $lastName, 'title' =>$title );
    $where = $this  ->getAdapter()
                    ->quoteInto('user_id = ?', $userId);
    $result = $this ->update($data, $where );
    //again ensure your method returns values that you want
    if (! $result) {
        return FALSE; //or exception or 0 or 'yankee doodle'
    } else {
    return TRUE; 
    }
}
满地尘埃落定 2025-01-11 22:51:12

如果您的 user_id 是自动递增的,则不要提供它。在 zend 上更新时,如果您的更新值与现有值相同,那么您将始终得到 0。只有当您真正更新数据库中的某些内容时,您才会在更新数据时得到 1。

If your user_id is auto increment then don't provide it. And while updating on zend if your updating value is same as the existing one then you will always get 0. You will get 1 while updating the data only when you are really updating something in your DB.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文