mysql查询在zend框架中不起作用

时间:2012-08-12 07:19:15

标签: php mysql zend-framework

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。但是,查询似乎没有被执行,因为每当我调用此函数时,我总是会刷新。

P.S这组查询是由另一名成员提供给我的。

3 个答案:

答案 0 :(得分: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
    }
}

答案 1 :(得分:0)

告诉别人你正在使用Zend框架会很有用。

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

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

答案 2 :(得分: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();
        }
}