How to get request type in PDO?

In the MySQL Reference Guide, there is a distinction between data definition statements and data manipulation statements.

Now I want to know if the query adds a database record, updates it, deletes one, or changes the structure of the table, etc. or, more precisely, the exact number of rows affected, but only if applicable.

For example, statement

SELECT *
FROM SomeTable
WHERE id=1 OR id=2

returns the number of rows affected (in this case 2 ), but SELECTnothing changes with the operator in the database, so the number will be 0 .

How to get request type?

+5
source share
3 answers

. . : " ". : ( )

  • columnCount() , . , rowCount(), .
  • columnCount() , , . , , .

.

$sth = $dbh->prepare ($stmt);
$sth->execute ();
if ($sth->columnCount () == 0)
{
    # there is no result set, so the statement modifies rows
     printf ("Number of rows affected: %d\n", $sth->rowCount ());
}
else
{
    # there is a result set
    printf ("Number of columns in result set: %d\n", $sth->columnCount ());
    $count = 0;
    while ($row = $sth->fetch (PDO::FETCH_NUM))
    {
    # display column values separated by commas
       print (join (", ", $row) . "\n");
       $count++;
    }
}
+8

, .

- , . , , :
, , , . , .

, , - affectedRows numRows. , .

+4

:

substr($statement->queryString, 0, strpos($statement->queryString, ' '));

where $ statement is a PDOStatement object, there are a few things to note before using this that $ statement is a PDOStatement object, we should probably also take strpos from the substr operator in case strpos returns false, which will probably cause an error, finally, it only works with one type of word operators, such as SELECT, INSERT, etc., and not with several types of statements, such as ALTER TABLE

-1
source

All Articles