API page: http://api.drupal.org/api/drupal/includes--database--database.inc/functi...
Describe the problem you have found:
Again there not described correctly what will be returned when nothing is found. I had this construct:
$result = db_select('flag_content', 'fc')
->fields('fc')
->condition('content_type', 'node')
->condition('content_id', $nid)
->condition('uid', $uid)
->execute()
->fetchAssoc()
;
and got a bool(false) returned. This is not mentioned in the documentation of fetchAssoc();
Comment | File | Size | Author |
---|---|---|---|
#15 | 1167218-db_fetch_doc-02.patch | 1.73 KB | kathyh |
#12 | 1167218-db_fetch_doc.patch | 808 bytes | droplet |
Comments
Comment #1
jhodgdonAre you saying that if there are no results, you get boolean FALSE returned, not an empty array?
Agreed that this function's doc should tell what happens if the query returns an empty result set. Needs to be fixed in d8 first and ported back to d7.
Comment #2
ro-no-lo CreditAttribution: ro-no-lo commented@jhodgdon: try this in any module:
I get an bool(false) returned. [Also with ->fetchAssoc()]
Comment #3
jhodgdonThanks! I traced this down -- fetchAssoc() is basically a wrapper for PDOStatement::fetch(), which is documented to return FALSE on failure (which I assume means including if there is nothing there):
http://www.php.net/manual/en/pdostatement.fetch.php
So we should add that to the doc for fetchAssoc(). Thanks for reporting this! Good project for a novice doc contributor...
Comment #4
droplet CreditAttribution: droplet commentedaccepts parameters fetch* methods are return ARRAY. (personal views)
Comment #5
jhodgdondroplet: I have no idea what you are trying to say in that comment. Can you please clarify?
Comment #6
droplet CreditAttribution: droplet commented@jhodgdon,
forgot it, just found that is not true.
these will return array() when nothing is found
DatabaseStatementBase::fetchAllAssoc
DatabaseStatementBase::fetchAllKeyed
PDOStatement->fetchAll
needs update all of them. :)
Comment #7
jhodgdonI don't think that is correct. I think they will all return FALSE when nothing is found -- see original report above and comments 1,2,3
Comment #8
droplet CreditAttribution: droplet commented#1~3 is right. fetchAssoc is return FALSE
BUT following return empty array()
DatabaseStatementBase::fetchAllAssoc
DatabaseStatementBase::fetchAllKeyed
PDOStatement->fetchAll
ref:
http://api.drupal.org/api/drupal/includes--database--database.inc/functi...
Comment #9
jhodgdonRight - I was confused between the fetch* and fetchAll* functions.
OK, so we need to document that:
fetch() [well, we don't document that -- it is PDOStatement], fetchAssoc() -- return FALSE if there is nothing there
fetchAllAssoc(), fetchAllKeyed() - return an empty array if there is nothing there
I'm not sure about what fetchAll() [which is a PDOStatement method, so we don't have to document it anyway - http://php.net/manual/en/pdostatement.fetchall.php] or fetchCol() do. The php site doesn't actually say what fetch() does, and fetchCol() just calls fetchAll(). So if someone wants to test out fetchAll() and fetchCol() that would be helpful.
I'm also not sure about fetchField(), which is a wrapper for http://us2.php.net/manual/en/pdostatement.fetchcolumn.php -- which also has no documentation for what the empty return value is, so if someone wants to investigate, that would also be helpful.
Comment #10
droplet CreditAttribution: droplet commentedokay. I do a real test.
return:
fetchAllAssoc => array
fetchAllKeyed => array
fetchAssoc => array
fetchCol => array
fetchField => string
rowCount => integer
NO RESULT:
return:
fetchAllAssoc => array
fetchAllKeyed => array
fetchAssoc => boolean
fetchCol => array
fetchField => boolean
rowCount => integer
Comment #11
jhodgdonPerfect, thanks!
So to be documented:
fetchAssoc(), fetchField() -- return FALSE if there is nothing there
fetchAllAssoc(), fetchAllKeyed(), fetchCol() - return an empty array if there is nothing there
*now* it's probably a good project for a novice. Just needs a patch!
Comment #12
droplet CreditAttribution: droplet commentedComment #13
jhodgdonGood try, but this patch has a couple of problems:
- Doesn't document all 5 functions listed in #11.
- I am not all that happy with the wording:
+ * A single field from the next record or FALSE is returned on failure.
Should be more like "A single field from the next record, or FALSE if there is no next record." I think that would be better?
Comment #14
kathyh CreditAttribution: kathyh commentedComment #15
kathyh CreditAttribution: kathyh commentedUpdated patch to document expected empty return values for fetchAssoc(), fetchField() fetchAllAssoc(), fetchAllKeyed() and fetchCol()
Comment #16
kathyh CreditAttribution: kathyh commentedComment #17
jhodgdonThanks very much -- that looks good to me! Should be D7/8.
Comment #18
webchickCommitted and pushed to 8.x. Thanks!
Comment #19
jhodgdonI guess it should still be backported to 7.x? Probably it will apply... I'll set version and then hit retest just in case.
Comment #20
jhodgdon#15: 1167218-db_fetch_doc-02.patch queued for re-testing.
Comment #21
webchickOops. This is why I shouldn't commit patches before coffee. :) I committed/pushed to 7.x too. no idea why that didn't get to my fingers. :P