Using PHP to query a MDB file, and return JSON

前端 未结 4 1112
你的背包
你的背包 2021-01-25 07:46

I have a Microsoft Access Database, and I am trying to query the table using PHP, and output valid JSON. I have an equivalent code for a MSSQL database, am I am trying to make m

相关标签:
4条回答
  • 2021-01-25 08:01

    You only need 1 loop, fetchAll is your iterable friend:

    while ($row = $data->fetchAll(SQLSRV_FETCH_ASSOC)) {
        $result[] = $row;   
    }
    
    0 讨论(0)
  • 2021-01-25 08:02

    odbc_connect doesn't return an object, it returns a resource. see (http://php.net/manual/en/function.odbc-connect.php) so you would need to do something like this.

     $db = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$dbName", $user, $password);
     $oexec = obdc_exec($db,$sql);
      $result[] = odbc_fetch_array($oexec);
    

    and then you can iterate over results..

    see also:

    http://www.php.net/manual/en/function.odbc-fetch-array.php http://www.php.net/manual/en/function.odbc-exec.php

    0 讨论(0)
  • 2021-01-25 08:21

    I finally figured it out.

    <?php
    // Location of database. For some reason I could only get it to work in
    // the same location as the site. It's probably an easy fix though
    $dbName = "dbName.mdb";
    $tName = "table";
    
    // Throws an error if the database cannot be found
    if (!file_exists($dbName)) {
        die("Could not find database file.");
    }
    
    // Connects to the database
    // Assumes there is no username or password
    $conn = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$dbName", '', '');
    
    // This is the query
    // You have to have each column you select in the format tableName.[ColumnName]
    $sql = "SELECT $tName.[ColumnOne], $tName.[ColumnTwo], etc...
            FROM $dbName.$tName";
    
    // Runs the query above in the table
    $rs = odbc_exec($conn, $sql);
    
    // This message is displayed if the query has an error in it 
    if (!$rs) {
        exit("There is an error in the SQL!");
    }
    
    $data = array();
    $i = 0;
    
    // Grabs all the rows, saves it in $data
    while( $row = odbc_fetch_array($rs) ) {
        $data[$i] = $row;
        $i++;
    } 
    
    odbc_close($conn); // Closes the connection
    $json = json_encode($data); // Generates the JSON, saves it in a variable
    ?>
    
    0 讨论(0)
  • 2021-01-25 08:22

    I use this code to get results from an ODBC query into a JSON array:

    $response = null;
    $conn = null;
    
    try {
      $odbc_name = 'myODBC'; //<-ODBC connectyion name as is in the Windows "Data Sources (ODBC) administrator"
    
      $sql_query = "SELECT * FROM table;";
    
      $conn = odbc_connect($odbc_name, 'user', 'pass');
      $result = odbc_exec($conn, $sql_query);
    
      //this will show all results:
      //echo odbc_result_all($result);
    
      //this will fetch row by row and allows to change column name, format, etc:     
      while( $row = odbc_fetch_array($result) ) { 
         $json['cod_sistema'] = $row['cod_sistema'];
         $json['sistema'] = $row['sistema'];
         $json['cod_subsistema'] = $row['cod_subsistema'];
         $json['sub_sistema'] = $row['sub_sistema'];
         $json['cod_funcion'] = $row['cod_funcion'];
         $json['funcion'] = $row['funcion'];
         $json['func_desc_abrev'] = $row['desc_abreviada'];
         $json['cod_tipo_funcion'] = $row['cod_tipo_funcion'];
    
         $response[] = array('funcionalidad' => $json);
      }
    
      odbc_free_result($result); //<- Release used resources
    
    } catch (Exception $e) {
       $response = array('resultado' => 'err', 'detalle' => $e->getMessage());
       echo 'ERROR: ',  $e->getMessage(), "\n";
    }
    odbc_close($conn);
    return $response;
    

    And finnally encoding the response in JSON format:

    echo json_encode($response);
    
    0 讨论(0)
提交回复
热议问题