ZF-3311: Zend_Db_Statement_Exception with message SQLSTATE[21000]: Cardinality violation: 1241 Operand should contain 1 column(s)
Description
The code
$sql = 'SELECT g.*, u2g.* FROM '.$this->_u2g.' AS u2g RIGHT JOIN '.$this->_name.' AS g ON u2g.uggid = g.groupid WHERE u2g.uguid = ? LIMIT 1';
$sql = $this->getAdapter()->quoteInto($sql, $userid);
return $this->fetchRow($sql);
generates
Zend_Db_Statement_Exception with message SQLSTATE[21000]: Cardinality violation: 1241 Operand should contain 1 column(s)
with the sql
SELECT g . * , u2g . *
FROM users2groups AS u2g
RIGHT JOIN groups AS g ON u2g.uggid = g.groupid
WHERE u2g.uguid = '5bc40bc6-7974-102b-8a2c-d23ebefec1c9'
LIMIT 1
The SQL returns valid results in phpMyAdmin
edit
mysql_connect('localhost', 'root', ''); mysql_select_db('zfapp'); $q = mysql_query($sql); print_r(mysql_fetch_assoc($q));
Produces valid results based on the above SQL.
more information http://phpbuilder.com/board/showthread.php/…
Comments
Posted by Simon Mundy (peptolab) on 2008-05-25T22:34:47.000+0000
I notice that you're using phpMyAdmin and the 'mysql' adapter - what adapter are you using for the framework? Pdo_Mysql or Mysqli? What happens if you try to use either of those adapters with that query?
The exception you're seeing indicates a driver-level exception rather than a Zend exception, so it would be good to narrow this down.
Posted by Travis Crowder (spechal) on 2008-05-26T00:06:29.000+0000
Hello Simon.
I am currently using the Pdo_Mysql adapter.
Switching to the Mysqli adapter yields:
'Zend_Db_Statement_Mysqli_Exception' with message 'Mysqli prepare error: Operand should contain 1 column(s)'
Thanks for your assistance.
Posted by Travis Crowder (spechal) on 2008-05-26T00:07:02.000+0000
MySQL DB Dump:
-- phpMyAdmin SQL Dump -- version 2.11.4
-- http://www.phpmyadmin.net
-- Host: localhost -- Generation Time: May 23, 2008 at 04:08 PM -- Server version: 5.0.51 -- PHP Version: 5.2.5
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--
-- Database:
zfapp--
-- Table structure for table
groupsDROP TABLE IF EXISTS
groups; CREATE TABLE IF NOT EXISTSgroups(groupidchar(36) collate latin1_general_ci NOT NULL,groupnamevarchar(48) collate latin1_general_ci NOT NULL,groupdescriptiontext collate latin1_general_ci NOT NULL, PRIMARY KEY (groupid) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;--
-- Dumping data for table
groupsINSERT INTO
groups(groupid,groupname,groupdescription) VALUES('2ce78951-7974-102b-8a2c-d23ebefec1c9', 'admin', 'This is the administrative group');--
-- Table structure for table
permissionsDROP TABLE IF EXISTS
permissions; CREATE TABLE IF NOT EXISTSpermissions(permidchar(36) collate latin1_general_ci NOT NULL,permnamevarchar(48) collate latin1_general_ci NOT NULL,permdescriptiontext collate latin1_general_ci NOT NULL, PRIMARY KEY (permid) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;--
-- Dumping data for table
permissionsINSERT INTO
permissions(permid,permname,permdescription) VALUES('991cdf89-7974-102b-8a2c-d23ebefec1c9', 'view', 'View things');--
-- Table structure for table
permissions2groupsDROP TABLE IF EXISTS
permissions2groups; CREATE TABLE IF NOT EXISTSpermissions2groups(pidchar(36) collate latin1_general_ci NOT NULL COMMENT 'permission id',gidchar(36) collate latin1_general_ci NOT NULL COMMENT 'group id', PRIMARY KEY (pid,gid) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;--
-- Dumping data for table
permissions2groupsINSERT INTO
permissions2groups(pid,gid) VALUES('991cdf89-7974-102b-8a2c-d23ebefec1c9', '2ce78951-7974-102b-8a2c-d23ebefec1c9');--
-- Table structure for table
permissions2usersDROP TABLE IF EXISTS
permissions2users; CREATE TABLE IF NOT EXISTSpermissions2users(pidchar(36) collate latin1_general_ci NOT NULL COMMENT 'permission id',uidchar(36) collate latin1_general_ci NOT NULL COMMENT 'user id', PRIMARY KEY (pid,uid) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;--
-- Dumping data for table
permissions2users--
-- Table structure for table
usersDROP TABLE IF EXISTS
users; CREATE TABLE IF NOT EXISTSusers(useridchar(36) collate latin1_general_ci NOT NULL,statusint(1) NOT NULL default '0',usernamevarchar(36) collate latin1_general_ci NOT NULL,peacheschar(40) collate latin1_general_ci NOT NULL, PRIMARY KEY (userid), KEYstatus(status) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;--
-- Dumping data for table
usersINSERT INTO
users(userid,status,username,peaches) VALUES('5bc40bc6-7974-102b-8a2c-d23ebefec1c9', 1, 'travis', '5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8');--
-- Table structure for table
users2groupsDROP TABLE IF EXISTS
users2groups; CREATE TABLE IF NOT EXISTSusers2groups(uguidchar(36) collate latin1_general_ci NOT NULL COMMENT 'user id',uggidchar(36) collate latin1_general_ci NOT NULL COMMENT 'group id', PRIMARY KEY (uguid,uggid) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;--
-- Dumping data for table
users2groupsINSERT INTO
users2groups(uguid,uggid) VALUES('5bc40bc6-7974-102b-8a2c-d23ebefec1c9', '2ce78951-7974-102b-8a2c-d23ebefec1c9');Posted by Travis Crowder (spechal) on 2008-06-09T08:30:05.000+0000
Any thoughts on this issue?
Posted by Dolf Schimmel (Freeaqingme) (freak) on 2009-02-28T18:10:29.000+0000
I cannot see how this issue was resolved with 'not an issue'. I just ran into this error too (1.7.x). If you google the error, you only get results referring to ZF, which makes it obvious that it has got to be somehow related to ZF. Will try to reproduce later.
Posted by Ralph Schindler (ralph) on 2009-05-19T12:37:37.000+0000
Can you please provide a small reproduction script of this? Something that will demonstrate behavior as well as with the db table it its querying against. As small as possible to trigger the error.
Thanks!
Posted by Pierre-Vncent Ledoux (pvledoux) on 2009-05-20T02:46:42.000+0000
Hi,
I have the same error with this query on 1.8.1.
(I know, it's a baaaad query, very bad...)
{quote} $sql = << NOW()) as story_media_count FROM newsroom_has_source Inner Join story ON newsroom_has_source.source_id = story.source_id Inner Join story_text ON (story.story_id = story_text.story_id AND story_text.lang_iso = '$lang_iso') Inner Join story_has_term ON story.story_id = story_has_term.story_id Inner Join term ON (story_has_term.term_id = term.term_id AND term.lang_iso = '$lang_iso') WHERE newsroom_has_source.newsroom_id = '$newsroom_id' AND story.story_published = 1 AND story.story_publication_date < NOW() AND story.story_expiration_date > NOW() AND ( LOWER(story.story_town) LIKE LOWER('%$query%') OR LOWER(story.story_country) LIKE LOWER('%$query%') OR LOWER(story.story_reference) LIKE LOWER('%$query%') OR LOWER(story_text.field_value) LIKE LOWER('%$query%') OR LOWER(term.term_value) LIKE LOWER('%$query%') ) EOT;
{/quote}
I have tried by removing the sub select and replace it with a simple column like term.term_id, but same error.
I use Pdo as adpater.
The query run in phpmyadmin and navicat 8.
Any idea ?
Thx!
Pv
Posted by Ralph Schindler (ralph) on 2009-05-20T10:23:28.000+0000
There must be a shorter test case than what you provided? Can we get that down to just a few simple statements, also will need the db to test against.
-ralph
Posted by Pierre-Vncent Ledoux (pvledoux) on 2009-05-20T11:01:48.000+0000
Yep sorry I was too lazy,
here is a shorter test:
EOT; $res_search = $this->_dba->fetchAll($sql);
and here is my table def:
-- Table structure for media
DROP TABLE IF EXISTS media; CREATE TABLE media ( media_id int(11) NOT NULL AUTO_INCREMENT, story_id int(11) NOT NULL, media_reference varchar(255) DEFAULT NULL, media_duration varchar(20) DEFAULT NULL, media_origin varchar(255) DEFAULT NULL, media_publication_date datetime DEFAULT NULL, media_expiration_date datetime DEFAULT NULL, media_order int(11) DEFAULT NULL, media_embedded tinyint(1) DEFAULT NULL, PRIMARY KEY (media_id), KEY fk_media_story (story_id), CONSTRAINT fk_media_story FOREIGN KEY (story_id) REFERENCES story (story_id) ON DELETE CASCADE ON UPDATE NO ACTION ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
-- Table structure for story
DROP TABLE IF EXISTS story; CREATE TABLE story ( story_id int(11) NOT NULL AUTO_INCREMENT, story_town text, story_country text, story_reference varchar(255) DEFAULT NULL, PRIMARY KEY (story_id) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
-- Records
INSERT INTO media VALUES ('1', '1', 'test1', null, null, null, null, null, null); INSERT INTO media VALUES ('2', '1', 'test2', null, null, null, null, null, null); INSERT INTO media VALUES ('3', '2', 'test11', null, null, null, null, null, null); INSERT INTO media VALUES ('4', '2', 'test22', null, null, null, null, null, null); INSERT INTO media VALUES ('5', '2', 'test33', null, null, null, null, null, null); INSERT INTO story VALUES ('1', 'test', 'TEST', 'TEST'); INSERT INTO story VALUES ('2', 'test2', 'test2', 'test2');
Thx!
Pv
Posted by Jan Juricek (pog) on 2009-05-27T06:37:46.000+0000
I confirm. ZF 1.8.1, following query:
SELECT hoardings_id, COUNT(camhashoa.hoardings_id) AS hoardings_count FROM campaigns_has_hoardings AS camhashoa LEFT JOIN hoardings AS hoa USING(hoardings_id)
It is being caused by the group function COUNT(...) - no matter if I specify one column, or all of them (*). The query simply joins two tables in a 1:N relation and counts the occurences of that "N" rows ... this is really a basic mysql stuff(!)
I have this query stored in $query string. When I execute this query with
$this->fetchRow($select);
then the error occurs. When I use
$this->getAdapter()->fetchAll($select, array(), Zend_Db::FETCH_OBJ);
everything runs fine. The query is also valid in every other usage environment, except zend framework. I tried to use the getAdapter->fetchAll hack, but then ran into trouble since I've been using the Zend_Paginator, which obviously doesn't except this bug here and fails.
Please, fix it ... I'm here to provide additional info.
Posted by Jean-Philippe Rivard Lauzier (jprl12) on 2009-07-14T18:15:55.000+0000
I have the same error with ZF 1.8.4
My SQL query work really great when I run it in phpMyAdmin, but it's impossible within the ZF without the hack of Jan Juricek (getAdapter->fetchAll ...).
Any update about this bug?
Posted by Ralph Schindler (ralph) on 2011-02-17T15:22:08.000+0000
Can you please supply a reproduction case similar to this: http://ralphschindler.com/2010/02/…
Attempt to keep the sql schema as small as possible to demonstrate the issue.
Posted by J-C Bubbendorf (bubbendorf) on 2011-08-02T23:29:33.000+0000
I have the same problem with this code: {quote}<?php $return = $db->update('#form_save', array('time' => new Zend_Db_Expr('NOW()'),'data' => $data), $db->quoteInto('form_id = ? AND form_url = ? AND user_id = ?', array($formId, $formUrl, $userId))); ?>{quote} With Zf 1.11.7.
Any update about this bug?