简体   繁体   中英

update zend db with multiple where

Is something wrong with my query when i am executing getting fatal error.

$select = $db->select()
    ->from('test', '*')
    ->where('user_id = ?', 1)
    ->where('url is NULL');
$Details = $db->fetchAll($select);

foreach ($Details as $row1) {
    $PublicId = $row1['public_id'];
    $data = array('password' => $randomString , 'flag' => 1);

    $db->update("test", $data, 'public_id =' . $PublicId);
    $pass = $row1['password']; 
}

Error is:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'wdefabcghbcla45' in 'where clause'' in C:\\xampp\\php\\PEAR\\Zend\\Db\\Statement\\Pdo.php:228 Stack trace: #0 C:\\xampp\\php\\PEAR\\Zend\\Db\\Statement\\Pdo.php(228): PDOStatement->execute(Array) #1 C:\\xampp\\php\\PEAR\\Zend\\Db\\Statement.php(300): Zend_Db_Statement_Pdo->_execute(Array) #2 C:\\xampp\\php\\PEAR\\Zend\\Db\\Adapter\\Abstract.php(479): Zend_Db_Statement->execute(Array) #3 C:\\xampp\\php\\PEAR\\Zend\\Db\\Adapter\\Pdo\\Abstract.php(238): Zend_Db_Adapter_Abstract->query('UPDATE test ...', Array) #4 C:\\xampp\\php\\PEAR\\Zend\\Db\\Adapter\\Abstract.php(634): Zend_Db_Adapter_Pdo_Abstract->query('UPDATE test ...', Array) #5 D:\\Zend Workspace\\Test\\testDelete.php(39): Zend_Db_Adapter_Abstract->update('test', Array, 'test_public_id...') #6 {main} Next exception 'Zend_Db_Statement_Exception' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'wdefabcghbcla45' in 'where clause'' in C:\\xampp \\php\\PEAR\\Zend\\Db\\Statement\\ in C:\\xampp\\php\\PEAR\\Zend\\Db\\Statement\\Pdo.php on line 234

The problem is your WHERE clause was ending up like WHERE public_id = wdefabcghbcla45 therefore MySQL was trying "wdefabcghbcla45" as a column instead of a value . You need to wrap it with quotes (single or double..doesn't matter).

Try it like this:

$where = $db->quoteInto('public_id = ?', $PublicId);
$db->update('test', $data, $where);

http://files.zend.com/help/Zend-Framework/zend.db.html#zend.db.adapter.quoting.quote-into

Try like

$data = array('password' => $randomString , 'flag' => 1);
$db->update( "test", $data, 'public_id ='.$PublicId );

When I tried like this, it worked. Thanks for helping me.

$data = array('password' => $randomString , 'flag' => 1);
$where = "public_id = '" . $PublicId."'";
$db->update( "test", $data, $where );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM