2

MySQL/i's $db->query('some query') will return a result set for successful SELECT, SHOW, DESCRIBE or EXPLAIN, or return true for successful INSERT, UPDATE, DELETE, DROP, etc.

As such, we can easily identify the "type" of query:

$result = $db->query('some query that we want to identify');

if($result === false){
    echo 'Error'; exit;
}

if($result === true){

    // query is a successful INSERT, UPDATE, DELETE, DROP, etc.

}else{ // else type of $result will be result set

    // query is a successful  SELECT, SHOW, DESCRIBE, EXPLAIN 

}

How can we do the above using PHP ADOdb?

I'm currently using:

$result = $db->Execute('some query');

if($result === false){
    echo 'Error'; exit;
}
if(get_class($result) === 'ADORecordSet_empty'){
    // query is a successful INSERT, UPDATE, DELETE, DROP, etc?
}else{
    // query is a successful  SELECT, SHOW, DESCRIBE, EXPLAIN ?
}

which seems to work, but it definitely feels fragile and "working against the API". Is there a better way to do it?

Are there built-in ADOdb functions to do it?

Pacerier
  • 86,231
  • 106
  • 366
  • 634
  • Could you just do a simple search on the sql string before hand to see which keyword it contains? – Toby Allen Nov 26 '15 at 20:07
  • @TobyAllen, That's going to fail in many use cases, e.g. if the table/column name contains that keyword and etc. It'll only work if a full blown MySQL source code interpreter is used, and even so the performance (of string parsing) would be outrageous when compared to what the MySQL API offers (`$result === true`). – Pacerier Nov 29 '15 at 11:01

2 Answers2

0

ADOdb returns an empty recordset object when the underlying API called in the DB-specific driver to execute the query returns true.

By default this means an ADORecordSet_empty object, but this may be different if custom recordset classes are in use (setting rsPrefix).

In other words, I don't think there is any other way of achieving what you want; you can make the code more portable by comparing against $db->rsPrefix . 'empty' instead of hardcoding 'ADORecordSet_empty'.

dregad
  • 1,150
  • 8
  • 21
0

You can use affected_rows() method

if ($DB->affected_rows() !== 0) {
 //true
}

http://devcodepro.com/view/70/1/ADOdb-Check-if-MySQL-query-executed-successfully

Peca
  • 1,892
  • 3
  • 20
  • 21