-1
class Application_Model_DbTable_Email extends Zend_Db_Table_Abstract
{

protected $_name = 'memberdetail';

    function getUserid($email)
    {   
        $subquery = $this->select()
        ->from('memberdetail', array('memberid'))
        ->where('email = ?', $email);

        $select = $this->select()
        ->from('usertable', array('userid'))
        ->join('memberdetail', 'usertable.userid = memberdetail.memberid')
        ->where('usertable.userid = ?', $subquery);

        $row = $select->query()->fetch();

        if (!$row) {
        echo "User id not found";
        } else {
        return $userid = $row['userid'];
        }


    }

}

嗨,我正在尝试从上述查询中返回用户 ID。但是,查询似乎没有被执行,因为每当我调用这个函数时我总是会刷新。

PS这组查询是由另一位成员给我的。

4

3 回答 3

1

看起来这已经被过度思考了。根据提供的信息usertable.userid = memberdetail.memberid在这种情况下,您的功能很简单。

/** this function assumes one and only one email will match a memberid
 *  this function can be improved by validating $email as existing in DB
 *  prior to querying DB, should be done at form level but could be accomplished here
 *  with Zend_Validate_Db_RecordExists()
 */
public function getUserIdFromEmail($email) {
$select = $this->select();
$select->where('email = ?',$email);

$row = $this->fetchRow($select);//fetch a single row
if (!is_null($row) {//fetchRow returns null if no row matched
    return $row->memeberid;//return memberid as string/integer = usertable.userid
    } else {
      //handle error
    }
}
于 2012-08-12T09:27:28.747 回答
0

告诉人们您正在使用 Zend 框架会很有用。

您需要为 $this 建立与数据库的连接,如此链接中的步骤 1 和 2 所述:

http://framework.zend.com/manual/en/zend.db.select.html/

于 2012-08-12T07:29:29.790 回答
0

你可以试试这个,如果它有帮助:

function getUserid($email){        

        $select = $this->select()
        ->setIntegrityCheck(false)
        ->from(array('m' => 'memberdetail'), array('b.userid'))
        ->join(array('b' => 'usertable'), 'b.userid = m.memberid')
        ->where('m.email = ?', $email);

        $row = $this->getAdapter()->fetchAll($select);

        if (!$row) {
        throw new Exception("User id not found");
        } else {
        return $row->toArray();
        }
}
于 2012-08-12T08:08:41.567 回答