Wednesday, 26 August 2020

Can't SELECT * (ORA-00932: inconsistent datatypes: expected CHAR got ADT)

Update Update: I can query by defining the column name... so the issue is with the query SELECT * but I have close to 100 columns in my table.. I do not want to hardcore them all in and maintain that list.. why can't I use SELECT * ??


I am simply trying to create a button; the front-end where they user can choose one of three tables to export as .csv file. On click of a choice -> the choice is passed back to my PHP via table:which; then specific table will be queried in the oracle database and downloaded in the browser. Below is my attempt returning an error.


jQuery front-end call (export button click):

$('#export' + '.' + crnt).click(function(){
      let which = crnt;
      window.location.href = "export?table="+which;
});

PHP via slim route:

<?php
    
$app->post('/export', function ($request, $response, $args) {    

    $which = $_POST['table'];
    
    $config = parse_ini_file(__DIR__ . '/../../xx/xx.ini');
    $dbuser = $config['dbuser'];
    $dbpass = $config['dbpass'];
    $dbhost = $config['dbhost'];
    $dbname = $config['dbname'];
    $constr = $dbhost.'/'.$dbname;
    $conn = oci_pconnect($dbuser, $dbpass, $constr, 'XXX');

    if ($which == 'table_1'){
        $sql = 'SELECT * FROM app.table_1';
        $columnsql = 'SELECT column_name FROM app.table_1';
    } else if ($which == 'table_2'){
        $sql = 'SELECT * FROM app.table_2';
        $columnsql = 'SELECT column_name FROM app.table_2';
    } else if ($which == 'table_3'){
        $sql = 'SELECT * FROM app.table_3';
        $columnsql = 'SELECT column_name FROM app.table_3';
    }

    $stmt = oci_parse($conn, $sql); 
    $columnstmt = oci_parse($conn, $columnsql); 
    oci_execute($stmt);
    oci_execute($columnstmt);

    $columnnames = oci_fetch_array($columnstmt, OCI_ASSOC);
    $output = fopen("php://output", "w");
    fputcsv($output, $columnnames);

    while ($row = oci_fetch_array($stmt, OCI_ASSOC)) {
        fputcsv($output, $row);
    }

    fclose($output);

    header("Content-Type: text/csv; charset=utf-8");
    header("Content-Disposition: attachment; filename=$which.csv");
    
    if (!@oci_execute($stmt)) {
       $error = oci_error($stmt);
       throw new Exception($error['message']);
    }

    if (!@oci_execute($columnstmt)) {
       $error = oci_error($columnstmt);
       throw new Exception($error['message']);
    }

});

Error 500: The above returns the below error in my dev tools network console -> preview response tab:

enter image description here

enter image description here


Update:

I have now updated all the post instances to -> get which fixed post method not allowed message but am still receiving the ORA inconsistent datatypes error ... is this something to do with my headers in the php? where I've included them? Do I need to specify a datatype in my ajax call as well? How do I handle the potential *ADT* datatype?



from Can't SELECT * (ORA-00932: inconsistent datatypes: expected CHAR got ADT)

No comments:

Post a Comment