Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I want to get the number of rows that have been add from an INSERT query but that give me this error:
Error: Call to a member function rowCount() on a non-object
This API page mentionned that we can get the number of affected lines for a DELETE, INSERT or UPDATE statement.
I use a MySQL databse.
Here's my code, the bug appear at the last line:
$query = db_insert('wysiwyg')
->fields(array('format', 'editor', 'settings'))
->values(array(
'format' => 'filtered_html',
'editor' => '',
))
->values(array(
'format' => 'full_html',
'editor' => 'tinymce',
))
->values(array(
'format' => 'plain_text',
'editor' => '',
))
->execute();
$num = $query->rowCount();
Thanks
Comments
Comment #1
renzor CreditAttribution: renzor commentedhttp://api.drupal.org/api/drupal/includes%21database%21query.inc/class/I...
When inserting data into the database, a queryobject (connection object) will be returned, where rowCount() doesn't exist.
It doesn't really make sense to count the amount of inserted rows, since you know the data that's being inserted.
Comment #2
gielfeldt CreditAttribution: gielfeldt commentedI beg to differ.
Consider this pseudo-example:
This scenario is not unlikely. However, the above code of course won't work, since $result does not implement rowCount(). You could argue, that one could just run a countQuery() on the first $query object, but that would be inefficient, especially on large data sets.
Instead, we are currently forced to do:
Now this isn't really that bad. Except when you for example need something like a LIMIT clause in the select query, which may implemented differently for some backends as there's no SQL99 standard for this.
I'm therefore changing this ticket to a feature request.
Comment #3
gielfeldt CreditAttribution: gielfeldt commentedMy bad. This feature is actually present in another form, using the return option Database::RETURN_AFFECTED: