completeamateur Posted October 11, 2008 Share Posted October 11, 2008 Hi there, I have run through the "Getting started with zend..." tutorial which went fairly smoothly. I'm now trying to run a simple query but Zend keeps throwing the error: Fatal error: Uncaught exception 'Zend_Db_Statement_Exception' with message 'SQLSTATE[21000]: Cardinality violation: 1241 Operand should contain 1 column(s)' in /Library/WebServer/Documents/library/Zend/Db/Statement/Pdo.php:238 Stack trace: #0 /Library/WebServer/Documents/library/Zend/Db/Statement.php(283): Zend_Db_Statement_Pdo->_execute(Array) #1 /Library/WebServer/Documents/library/Zend/Db/Adapter/Abstract.php(430): Zend_Db_Statement->execute(Array) #2 /Library/WebServer/Documents/library/Zend/Db/Adapter/Pdo/Abstract.php(220): Zend_Db_Adapter_Abstract->query(Object(Zend_Db_Table_Select), Array) #3 /Library/WebServer/Documents/library/Zend/Db/Table/Abstract.php(1189): Zend_Db_Adapter_Pdo_Abstract->query(Object(Zend_Db_Table_Select)) #4 /Library/WebServer/Documents/library/Zend/Db/Table/Abstract.php(1044): Zend_Db_Table_Abstract->_fetch(Object(Zend_Db_Table_Select)) #5 /Library/WebServer/Documents/villa4life/application/controllers/BodymoorheathController.php(10): Zend_Db_Table_Abstract->fetchAll('SELECT * FROM c...') #6 in /Library/WebServer/Documents/library/Zend/Db/Statement/Pdo.php on line 238 I believe this is the problem code: $this->view->comments = $comments->fetchAll('SELECT * FROM comments ORDER BY dateTime'); Any suggestions. I'm very confused by the fundamentals of executing queries in Zend. Quote Link to comment https://forums.phpfreaks.com/topic/127991-confused-zend_db/ Share on other sites More sharing options...
kazil Posted October 12, 2008 Share Posted October 12, 2008 I'm not sure what's the error about, so let's try a different approach Why don't you use the model for working with the database? That's the point of the MVC. I'll try to write a basic model for your comments table, based on the info provided. This model should go in: /Library/WebServer/Documents/villa4life/application/models/Comments.php <?php class Comments extends Zend_Db_Table { protected $_name = 'comments'; // The table name protected $_primary = 'id_comment'; // The name of the primary column protected $_schema = 'database_name'; // Name of the database public function getAll() { $sql = $this->select()->from('comments'); $rowsetComments = $this->fetchAll($sql); return $rowsetComments; } } ?> Now, the 'id_comment', 'comment', 'author' and the 'date' columns here are fictitious, change them to suit your scenario. To use it in the controller, use it like this: // some where in some action function... $comments = new Comments(); $this->view->comment = $comments->getAll(); And that should do it... Quote Link to comment https://forums.phpfreaks.com/topic/127991-confused-zend_db/#findComment-663327 Share on other sites More sharing options...
completeamateur Posted October 12, 2008 Author Share Posted October 12, 2008 Thanks Kazil, that has worked a treat. I'm still learning! Quote Link to comment https://forums.phpfreaks.com/topic/127991-confused-zend_db/#findComment-663382 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.