PHP $editor->db()->raw() rowcount after UPDATE/DELETE

PHP $editor->db()->raw() rowcount after UPDATE/DELETE

hapihapi Posts: 18Questions: 3Answers: 0

Hi, is there a way to get the number of rows effected after an UPDATE or DELETE statement.

->count() seems working only, if there is a result array after a SELECT

$sql = "DELETE FROM TABLE_X WHERE Y = :Y";
$result = $editor->db()->raw()
->bind(':Y', $Y)
->exec($sql);

This question has an accepted answers - jump to answer

Answers

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    PDOStatement::rowCount()
    https://www.php.net/manual/en/pdostatement.rowcount.php

    Not really a DataTables issue.

  • hapihapi Posts: 18Questions: 3Answers: 0

    How can I access the statement. The result does not include rowCount() function?

    Uncaught Error: Call to undefined method DataTables\Database\Driver\OracleResult::rowCount()

    I have problems with dead locks, if I use my own connection, I need to work in the same session of the Editor backend.

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    I assumed you were using MySQL. Presumably Oracle has an equivalent statement.
    Try an Oracle forum.

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    rowCount probably will work with Oracle (it's a standard PDO method), but it is a PDO method, not one which is on our Result object.

    What you probably need to do here is get the PDO object:

    $pdo = $db->resource();
    

    and then you can use the regular PDO methods that PHP provides.

    Regards,
    Allan

  • hapihapi Posts: 18Questions: 3Answers: 0

    Thanks! Still not working. Indeed for Oracle it should work.

    $pdo = $editor->db()->resource();
    $count_updates += $pdo->rowCount();
    
    Fatal error</b>:  Uncaught Error: Call to a member function rowCount() on resource in my code
    Stack trace:
    #0 [internal function]: {closure}(Object(DataTables\Editor), '735', Array, Array)
    #1 /opt/rzbtfint/HTTPServer/htdocs/php/inc/datatables/lib/Editor.php(2204): call_user_func_array(Object(Closure), Array)
    #2 /opt/rzbtfint/HTTPServer/htdocs/php/inc/datatables/lib/Editor.php(1260): DataTables\Editor-&gt;_trigger('postEdit', '735', Array, Array)
    #3 /opt/rzbtfint/HTTPServer/htdocs/php/inc/datatables/lib/Editor.php(1034): DataTables\Editor-&gt;_update('735', Array)
    #4 /opt/rzbtfint/HTTPServer/htdocs/php/inc/datatables/lib/Editor.php(703): DataTables\Editor-&gt;_process(Array)
    #5 /opt/rzbtfint/HTTPServer/htdocs/php/pages/tools/calendar.php(741): DataTables\Editor-&gt;process(Array)
    #6 /opt/rzbtfint/HTTPServer/htdocs/php/index.php(192): include_once('/opt/rzbtfint/H...')
    #7 {main}
      thrown in ... my code
    
  • hapihapi Posts: 18Questions: 3Answers: 0

    Sorry, probably I know now why it does not work. I use a fake PDO Class, as PDO is not installed on my Server. The Fake Class probably does not work here.

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Answer ✓

    Actually it is probably because $pdo there is not a PDOStatement - it is just a PDO instance. You need to do the whole stmt() stuff as you usually would with the PDO class - we don't provide access to the PDO statements that our libraries use via an API method.

    Allan

  • hapihapi Posts: 18Questions: 3Answers: 0

    Ahhh many thanks, this hint brought me now to the right direction.

    However it was not a PDO connection, but a OCI8 instead.

    $connOCI = $editor->db()->resource();
    $stmt = oci_parse($connOCI, $sql);
    oci_bind_by_name($stmt, ":ID", $id);
    oci_execute($stmt);
    $count_updates += oci_num_rows($stmt);
    oci_free_statement($stmt);
    

    Many thanks!

This discussion has been closed.