1

我有一个 Microsoft Access 数据库,我正在尝试使用 PHP 查询表,并输出有效的 JSON。我有一个 MSSQL 数据库的等效代码,我是否正在尝试让我的代码做同样的事情,但只是针对 Access 数据库。

这是MSSQL代码

$myServer = "server";
$myDB = "db";
$conn = sqlsrv_connect ($myServer, array('Database'=>$myDB));

$sql = "SELECT *
        FROM db.dbo.table";

$data = sqlsrv_query ($conn, $sql);

$result = array();   

do {
    while ($row = sqlsrv_fetch_array ($data, SQLSRV_FETCH_ASSOC)) {
        $result[] = $row;   
    }
} while (sqlsrv_next_result($data));

$json = json_encode ($result);

sqlsrv_free_stmt ($data);
sqlsrv_close ($conn); 

这是我为 MDB 文件尝试的内容

$dbName = "/filename.mdb";

if (!file_exists($dbName)) {
    die("Could not find database file.");
}

$db = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$dbName", $user, $password);

$sql = "SELECT *
        FROM cemetery";

$data = $db->query($sql); // I'm getting an error here
$result = array();   

// Not sure what do do for this part...
do {
    while ($row = fetch($data, SQLSRV_FETCH_ASSOC)) {
        $result[] = $row;   
    }
} while (sqlsrv_next_result($data));

$json = json_encode ($result);

我有点按照这个尝试连接到数据库: http: //phpmaster.com/using-an-access-database-with-php/

目前这给了我一个 500 内部服务器错误。我期待这样的字符串保存在变量中$json

[
    {
        "col1":"col value",
        "col2":"col value",
        "col3":"col value",
    },
    {
        "col1":"col value",
        "col2":"col value",
        "col3":"col value",
    },
    {
        etc...
    }
]

有人可以帮我移植上面的 MSSQL 代码,以便我可以将它与 MDB 数据库一起使用吗?谢谢您的帮助!


编辑:我正在一一注释掉这些行,它在该行抛出了 500 错误$data = $db->query($sql);。我查看了错误日志,我得到了错误Call to a member function query() on a non-objectextension=php_pdo_odbc.dll我的php.ini 文件中已取消注释该行。任何人都知道问题可能是什么?

4

4 回答 4

0

您只需要 1 个循环, fetchAll 是您的可迭代朋友:

while ($row = $data->fetchAll(SQLSRV_FETCH_ASSOC)) {
    $result[] = $row;   
}
于 2013-08-02T15:25:45.847 回答
0

odbc_connect 不返回对象,而是返回资源。请参阅(http://php.net/manual/en/function.odbc-connect.php)所以你需要做这样的事情。

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

然后你可以迭代结果..

也可以看看:

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

于 2013-08-02T19:07:06.073 回答
0

我终于弄明白了。

<?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
?>
于 2013-08-27T19:22:21.807 回答
0

我使用此代码将 ODBC 查询的结果获取到 JSON 数组中:

$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;

最后以 JSON 格式编码响应:

echo json_encode($response);
于 2014-07-22T18:07:53.110 回答