Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我的Zend框架“引用”混乱_Php_Sql_Zend Framework_Quoting - Fatal编程技术网

Php 我的Zend框架“引用”混乱

Php 我的Zend框架“引用”混乱,php,sql,zend-framework,quoting,Php,Sql,Zend Framework,Quoting,我有一个可能非常简单的问题,我在Zend框架手册或其他地方找不到令人满意的主观答案 我有很多方法可以将php变量交给sql查询,因此我失去了概述,可能对一般的引用缺乏了解 准备好的发言 我明白,使用这个解决方案,我不需要引用任何东西,因为db为我处理这个问题 通过API查询Zend_Db_表和_行对象 $users=新用户 a) $users->fetchRow('userID = ' . $userID); b) $users->fetchRow('userID = ' . $

我有一个可能非常简单的问题,我在Zend框架手册或其他地方找不到令人满意的主观答案

我有很多方法可以将php变量交给sql查询,因此我失去了概述,可能对一般的引用缺乏了解

准备好的发言 我明白,使用这个解决方案,我不需要引用任何东西,因为db为我处理这个问题

通过API查询Zend_Db_表和_行对象 $users=新用户

a) $users->fetchRow('userID = ' . $userID);  
b) $users->fetchRow('userID = ' . $users->getAdapter()->quote($userID, 'INTEGER'));  
c) $users->fetchRow('userID = ?', $userID);  
d) $users->fetchRow('userID = ?', $users->getAdapter()->quote($userID, 'INTEGER'));  
问题 我理解a不好,因为它根本没有被引用。但是其他版本呢,最好的是什么?c是否被视为语句并自动引用,还是在使用时需要使用d?标识符?

免责声明:此信息自本答案的原始发布日期起有效。ZF经常更改,这些信息可能会随着未来版本的发布而过时,但出于存档目的,这些信息将保持不变

如果将字符串传递给正在执行的Zend_Db_Table_Abstract子类的fetchRow方法,它将被视为Zend_Db_Table_Select实例的where部分

换句话说,Zend_Db_表在内部执行以下操作:

if (!($where instanceof Zend_Db_Table_Select)) {
    $select = $this->select();

    if ($where !== null) {
        $this->_where($select, $where);
    }
所以…:

a) $users->fetchRow('userID = ' . $userID);  
完全没有引用

b) $users->fetchRow('userID = ' . $users->getAdapter()->quote($userID, 'INTEGER'));  
被手动引用为整数

c) $users->fetchRow('userID = ?', $userID);  
由Zend_Db_适配器自动引用::quoteInto

实际上是双引号,一次是由您引用,一次是通过自动引用

就best而言,我建议使用选项C。框架将自动调用参数化值的quoteInto

请记住:您可以将Zend_Db_Table_Select或Zend_Db_Select的实例传递给fetchRow方法

同样,在Zend_Db_Table_Abstract的子类中,看起来是这样的:

$this->fetchRow($this->select()->where('userID = ?', $userID));
这样做的好处是,您可以构造更复杂的查询,因为您可以控制比SQL查询的WHERE子句多得多的查询。理论上,你可以很容易地做到:

$select = $this->select()->where('userID = ?', $userID)
                         ->join(array('sat' => 'superAwesomeTable'), array('sat.user_id = userID', array('superAwesomeColumn'));

$this->fetchRow($select);
注意:如果传递了Zend_Db_Select的实例,fetchRow方法的行为与fetchAll完全相同,只是它在内部调用Select对象的limit方法,参数为1。

我已经习惯了

$where = $this->getAdapter()->quoteInto('name = ?', $name);
$this->fetchRow($where);

好吧,这似乎是一个相当糟糕的选择,因为它引用了两次,甚至没有正确地使用Zend_Db_Select。它肯定没有引用两次;AFAIK对于简单的查询不需要使用Z_DB_Select。从对象构建SQL语句需要花费资源和时间;
$select = $this->select()->where('userID = ?', $userID)
                         ->join(array('sat' => 'superAwesomeTable'), array('sat.user_id = userID', array('superAwesomeColumn'));

$this->fetchRow($select);
$where = $this->getAdapter()->quoteInto('name = ?', $name);
$this->fetchRow($where);