2013-08-02 20 views
1

我有一個Microsoft Access數據庫,並且我試圖使用PHP查詢表,並輸出有效的JSON。我有一個MSSQL數據庫的等效代碼,我試圖讓我的代碼做同樣的事情,但只是爲Access數據庫。使用PHP查詢MDB文件並返回JSON

這裏是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-object。我已經在我的php.ini文件中取消註釋extension=php_pdo_odbc.dll。任何人都知道問題可能是什麼?

+1

學會做回聲使用調試,並找到你的錯誤的地方。一個我已經發現:'while($ row = fetch($ data,SQLSRV_FETCH_ASSOC))'應該是'while($ row = $ data-> fetch(PDO :: FETCH_ASSOC))''。 'do {..} while'需要被移除。只要看看你發佈的例子,它的全部那裏 – x4rf41

+0

請更新你的問題,告訴我們你期望發生什麼,現在發生了什麼,以及你試圖解決它。考慮包括您收到的任何錯誤消息,以及您用來幫助解決問題以及他們未能如何回答您的問題的資源說明。 –

+0

當我運行這個時,它給了我一個500內部服務器錯誤,所以我看不到任何回顯的字符串(即使進行了您建議的更改後,我也會得到此錯誤)。我將MDB文件放在與我的網站相同的目錄中,現在也僅用於測試。我已經更新了這個問題,喬治! – TFischer

回答

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 
?> 
0

你只需要1環, 使用fetchall是您的朋友可迭代:

while ($row = $data->fetchAll(SQLSRV_FETCH_ASSOC)) { 
    $result[] = $row; 
} 
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

+0

現在,這給了我一個警告(' odbc_fetch_array()期望參數1是資源,布爾值在C:\\ ...中給出)和一個致命錯誤('調用成員函數fetchAll() ) – TFischer

+0

對不起,我遺漏了錯誤檢查,等等。你需要檢查所有的呼叫都不會返回錯誤,如果他們這樣做。調用odbc_error來找出錯誤是什麼。我的猜測是,當傳遞給obdc_fetch_array時,oexec是錯誤的。在查看你的帖子,以及你正在使用的教程中使用的PDO與使用ODBC_ *函數不同 – Doon

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; 

而且finnally編碼JSON格式的響應:

echo json_encode($response);