[RESOLVED] Cardinality violation
Results 1 to 13 of 13

Thread: [RESOLVED] Cardinality violation

  1. #1
    Fatal Error Line 0 Kudose's Avatar
    Join Date
    Feb 2003
    Location
    Kansas City
    Posts
    3,589

    resolved [RESOLVED] Cardinality violation

    Hello all.

    So I've got this error:

    Code:
    SQLSTATE[21000]: Cardinality violation: 1241 Operand should contain 1 column(s)
    With this SQL

    Code:
    SELECT `login`, `email` FROM `myusers` WHERE `id` = '9f1ac727-587f-4ae4-8008-6054566ef219' LIMIT 1
    The table is simple

    Code:
    id
    login
    password
    email
    Why would I get that error?

    I am thinking it is a Zend Framework bug because the SQL works in phpMyAdmin.

    PHP Code:
    $this->query 'SELECT `login`, `email` FROM `'.$this->_name.'` WHERE `id` = \''.$this->user->id.'\' LIMIT 1';
          try {
            
    $userInfo $this->fetchRow($this->query);
            
    $this->user->username $userInfo->login
            
    $this->user->email $userInfo->email;
            return 
    true;
          } catch(
    Exception $e) {
            
    $this->setAdminError($e->getMessage().'<br />'.$this->query__METHOD____LINE____FILE__);
            
    $this->publicMsg $this->translations->translate('error500');
            return 
    false;
          } 
    TIA!
    Problem Solved? Mark this thread resolved using the Thread Tools.
    Apache HQ | PHP HQ | MySQL HQ | LinkedIn
    0100001001111001011101000110010100100000011011010110010100100001

  2. #2
    To code, or not to code? jkurrle's Avatar
    Join Date
    Jul 2004
    Location
    Gainesville, VA
    Posts
    520
    What happens if you drop "limit 1" from the sql query. Just curious...
    99 little bugs in the code, 99 bugs in the code,
    fix one bug, compile it again...
    101 little bugs in the code....

  3. #3
    To code, or not to code? jkurrle's Avatar
    Join Date
    Jul 2004
    Location
    Gainesville, VA
    Posts
    520
    is "id" a keyword? I don't remember. If it is, change the select statement to

    SELECT `login`, `email` FROM `myusers` WHERE `myusers.id` = '9f1ac727-587f-4ae4-8008-6054566ef219' LIMIT 1
    99 little bugs in the code, 99 bugs in the code,
    fix one bug, compile it again...
    101 little bugs in the code....

  4. #4
    Fatal Error Line 0 Kudose's Avatar
    Join Date
    Feb 2003
    Location
    Kansas City
    Posts
    3,589
    Thanks for the suggestions, but id is not a reserved word and encapsulating the string in backticks tells MySQL to use the string as a column instead of a keyword.
    Problem Solved? Mark this thread resolved using the Thread Tools.
    Apache HQ | PHP HQ | MySQL HQ | LinkedIn
    0100001001111001011101000110010100100000011011010110010100100001

  5. #5
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    13,902
    I can't see from the query provided how it could result in that error. Usually it occurs when there is a mismatch resulting from incorrect use of parentheses and usually in conjunction with sub-queries, which obviously does not seem to apply here.

    I'm not familiar with the Zend framework, but should there be some method call to execute the query prior to the fetchRow() call?
    Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." ~ from Nation, by Terry Pratchett

    "But the main reason that any programmer learning any new language thinks the new language is SO much better than the old one is because hes a better programmer now!" ~ http://www.oreillynet.com/ruby/blog/...ck_to_p_1.html


    eBookworm.us

  6. #6
    To code, or not to code? jkurrle's Avatar
    Join Date
    Jul 2004
    Location
    Gainesville, VA
    Posts
    520
    I think cardinality has to do with your index:

    Cardinality
    The number of unique values in the index. This is updated by running ANALYZE TABLE or myisamchk -a. Cardinality is counted based on statistics stored as integers, so it's not necessarily accurate for small tables. The higher the cardinality, the greater the chance that MySQL uses the index when doing joins.
    99 little bugs in the code, 99 bugs in the code,
    fix one bug, compile it again...
    101 little bugs in the code....

  7. #7
    To code, or not to code? jkurrle's Avatar
    Join Date
    Jul 2004
    Location
    Gainesville, VA
    Posts
    520
    SyBase has a PDF that discusses your error in section 4-2.
    99 little bugs in the code, 99 bugs in the code,
    fix one bug, compile it again...
    101 little bugs in the code....

  8. #8
    Fatal Error Line 0 Kudose's Avatar
    Join Date
    Feb 2003
    Location
    Kansas City
    Posts
    3,589
    *sigh*

    I'm back, but this time I have a whole new query!

    Code:
    SELECT `g`.*, `u2g`.* FROM `users2groups` AS `u2g` RIGHT JOIN `groups` AS `g` ON `g`.`groupid` = `u2g`.`uggid` WHERE `u2g`.`uguid` = '5bc40bc6-7974-102b-8a2c-d23ebefec1c9'
    SQLSTATE[21000]: Cardinality violation: 1241 Operand should contain 1 column(s)
    From the SyBase PDF linked above:
    Cardinality violations occur when a query that should return only a
    single row returns more than one row to an Embedded SQL™
    application.
    That makes sense, but it doesn't apply to my situation, from what I can tell.

    Can anyone see what I am doing wrong? The SQL executes in phpMyAdmin (and returns only one row), just not through the Zend framework. It shouldn't matter that it is going through ZF though, since it is returning a valid SQL error ZF is doing it's job.

    ## edit
    I found this on page 159, section 5-37 under lock manager messages, code 1241

    A semaphore cursor refers to a different semaphore than the one it is linked to.
    In English, I think that means:
    The column you are accessing refers to a column other than the one you want.

    ... how would I resolve that?
    Last edited by Kudose; 05-22-2008 at 03:46 PM.
    Problem Solved? Mark this thread resolved using the Thread Tools.
    Apache HQ | PHP HQ | MySQL HQ | LinkedIn
    0100001001111001011101000110010100100000011011010110010100100001

  9. #9
    Fatal Error Line 0 Kudose's Avatar
    Join Date
    Feb 2003
    Location
    Kansas City
    Posts
    3,589
    Ok, this has to be a ZF bug.

    Code:
    mysql_connect('localhost', 'root', '');
          mysql_select_db('zfapp');
          $q = mysql_query($sql);
          print_r(mysql_fetch_assoc($q));
    Produces valid results.

    Where $sql equals the SQL in the previous post.

    ## edit
    Reported
    Last edited by Kudose; 05-22-2008 at 04:19 PM.
    Problem Solved? Mark this thread resolved using the Thread Tools.
    Apache HQ | PHP HQ | MySQL HQ | LinkedIn
    0100001001111001011101000110010100100000011011010110010100100001

  10. #10
    Junior Member
    Join Date
    Jul 2008
    Posts
    1
    Quote Originally Posted by Kudose
    Hello all.
    Code:
    ... Operand should contain 1 column(s)
    With this SQL
    Code:
    SELECT `login`, `email` FROM `myusers` WHERE `id` = '9f1ac727-587f-4ae4-8008-6054566ef219' LIMIT 1
    I am thinking it is a Zend Framework bug because the SQL works in phpMyAdmin.

    PHP Code:
    $this->query 'SELECT `login`, `email` FROM `'.$this->_name.'` WHERE `id` = \''.$this->user->id.'\' LIMIT 1';
          try {
            
    $userInfo $this->fetchRow($this->query);
     ....
          } catch(
    Exception $e) {
          } 
    I've just come across this problem - the key to it was in the manual:

    PHP Code:
    $stmt $db->query('SELECT * FROM bugs');
    $rows $stmt->fetchAll(); 
    Run the SQL through query(), and then fetch the rows you want.

  11. #11
    NMaOtBG bpat1434's Avatar
    Join Date
    Oct 2004
    Location
    Around 255.255.255.0
    Posts
    7,850
    Kudose, I don't think it's a bug but rather you're missing the actual query to the database.

    You have to execute $db->query($this->query) otherwise you're not querying the database. At least that's from my limited knowledge of ZF database stuff.

  12. #12
    Fatal Error Line 0 Kudose's Avatar
    Join Date
    Feb 2003
    Location
    Kansas City
    Posts
    3,589
    You guys are correct.

    I was able to get it figured out by creating a Zend_Db_Table_Select object and passing it to Zend_Db_Table
    Problem Solved? Mark this thread resolved using the Thread Tools.
    Apache HQ | PHP HQ | MySQL HQ | LinkedIn
    0100001001111001011101000110010100100000011011010110010100100001

  13. #13
    Junior Member
    Join Date
    May 2009
    Posts
    1
    Hi,

    I also ran through the same error message while trying to run the "SELECT * FROM <table_name>" query. After a lot search on the net I resolved the error. The query was:

    $select = $this->select()->distinct(true);

    and I tried to run this built this way:

    $result = $this->fetchAll($select, array(), Zend_Db::FETCH_OBJ);

    The part I was missing was "getAdapter()" method. I added that:

    $result = $this->getAdapter()->fetchAll($select, array(), Zend_Db::FETCH_OBJ);

    That fixed the problem. Hope this help for the guys facing the same kind of problem.

    Thanks
    Asit

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •