segaps
segaps

Reputation: 57

php function db2_exec not running specific DB2 queries

I'm trying to query a DB2 database via a php script.

   if ($conn) {
    $stmt = db2_prepare($conn, $sql);
   if ($stmt) {
      $success = db2_execute($stmt);
      if (!$success) {
         $result[]=  "exec errormsg1: " .db2_stmt_errormsg($stmt);
         echo implode(' ',$result);
         $message = implode(' ',$result); ?>
         <script>
         window.alert(' <?php echo $message; ?>');
         </script>
      <?php 
      }
      else
      {
        while ($row = db2_fetch_both($stmt)) 
        {
          if ($format == "human"){
                $result[] = implode("       ", $row);
            } else {
                $result[] = implode(",", $row);

            }
        }

I'm running into issues with certain queries being ran using either db2_exec or db2_execute.

I was having issues with the code above with meta data type queries though and found some workarounds , like for example if my sql was hardcoded to

$query = "describe table schemaID.tablename";

I would need to modify it by encasing it in

$sql = "call SYSPROC.ADMIN_CMD('".$query."')";

so while that works for the describe function I'm left scratching my head over what to do with these type of commands like

list tables for schema schemaID

which errors in

exec errormsg1: [IBM][CLI Driver][DB2/AIX64] SQL0104N An unexpected token "tables" was found following "list ". Expected tokens may include: "JOIN ". SQLSTATE=42601

or

get db cfg

which errors in

exec errormsg1: [IBM][CLI Driver][DB2/AIX64] SQL0104N An unexpected token "db" was found following "GET". Expected tokens may include: "STMM". SQLSTATE=42601

from the error messages I see it's having an issue with delimiters but I'm not sure exactly why since I can get other queries to work fine.

Also if I attempt to run those commands via the command line they'll both work fine without any issue.

Is there something I'm missing regarding how to correctly query DB2 in php ? Ideally I'd love to just be able to run the same queries exactly as I would from the command line and not face these issues

Upvotes: 1

Views: 485

Answers (1)

data_henrik
data_henrik

Reputation: 17118

You can execute DB2 SQL statements with that PHP code. What you are asking for is to also run DB2 CLP commands within your code which is not possible by definition.

You could try to find solutions in the DB2 manual for some of the tasks, e.g., using the ADMIN_CMD stored procedure. For some commands like "LIST TABLES" there is a workaround by directly querying the DB2 system catalog, e.g., the table SYSCAT.TABLES.

Upvotes: 1

Related Questions