2013-06-03 113 views
-2

我真的很努力用MSSQL數據庫填充數據表(它是在SQL Server 2012 Express中管理的)。我已經從他們的網站(他們的服務器端PHP與ODBC腳本)嘗試了幾個腳本;然而,沒有人工作。我偶然發現了一個我現在嘗試使用的帖子(http://www.datatables.net/forums/discussion/7359/mssql-as-data-source...-im-confused/p1)。這裏是我的代碼用MSSQL數據填充數據表

<table class="display" id="table1"> 
    <thead> 
     <tr> 
      <th>PRIMARY HEADINGS</th> 
      <th>PRIMARY CLASS CODE</th> 
      <th>PH DESCRIPTION</th> 
     </tr> 
</thead> 
    <tbody> 
     <?php 
      include("scripts/script.php"); 

      $sql= "SELECT * 
      FROM dbo.PriClass 
      ORDER BY ID"; 
      $result = sqlsrv_query($conn, $sql); 

      while($value=sqlsrv_fetch_array($result)){ 
       echo "<tr>", 
       "<td>$value[Primary Headings]</td>", 
       "<td>$value[Primary Class Code]</td>", 
       "<td>$value[PH Description]</td>", 
       "</tr>"; 
      } 
     ?> 
    </tbody> 
    <tfoot> 
     <tr> 
      <th>PRIMARY HEADINGS</th> 
      <th>PRIMARY CLASS CODE</th> 
      <th>PH DESCRIPTION</th> 
     </tr> 
</tfoot> 
</table> 

這裏是PHP文件

<?php 
    $hostname = "SERVERNAME"; 
    $username = "USERNAME"; 
    $password = "PASSWORD"; 
    $dbname = "DBNAME"; 
    $connectionInfo = array("UID"=>$username, "PWD"=>$password, "Database"=>$dbname); 
    $conn = sqlsrv_connect($hostname, $connectionInfo); 

    if($conn === false) { 
     echo "Unable to connect to database."; 
     die(print_r(sqlsrv_errors(), true)); 
    } 
?> 

因此,這段代碼的輸出是:

http://i.imgur.com/iFVa2U5.png

我知道旁邊沒有關於PHP和數據庫,所以我不知道如何解決這個問題。理想情況下,我想有另一個while循環遍歷列。例如。

while(loops through each row) { 
    echo "<tr>", 

    while(loops through each column) { 
     "<td>value of the cell</td>", 
    } 

    "</tr>"; 
} 

這樣我可以輕鬆地重新使用不同大小的數據庫的代碼。我也是,我也必須有thead和tfoot其他的PHP代碼。我正在測試的數據庫有4列(一個是ID,僅用於索引)和14行。我正在WebMatrix 3中構建它,並且我已經通過它連接了我的數據庫。如果有人能幫助我,那會很好。謝謝


編輯:我解決了這個問題(謝謝你關閉這個......)。答案不依賴於服務器端處理,但我不認爲我需要它。我只是從MSSQL數據庫讀取數據,然後用它填充表格。

.PHP

<?php 
// This is for SQL Authentication. I've added instructions if you are using Windows Authentication 

// Uncomment this line for troubleshooting/if nothing displays 
//ini_set('display_errors', 'On'); 

// Server Name 
$myServer = "SRVR"; 

// If using Windows Authentication, delete this line and the $myPass line as well. 
// SQL Server User that has permission to the database 
$myUser = "usr"; 

// SQL Server User Password 
$myPass = "Passwd1"; 

// Database 
$myDB = "TestDB"; 

// If using Windows Authentication, get rid of, "'UID'=>$myUser, 'PWD'=>$myPass, " 
// Notice that the latest driver uses sqlsrv rather than mssql 
$conn = sqlsrv_connect($myServer, array('UID'=>$myUser, 'PWD'=>$myPass, 'Database'=>$myDB)); 

// Change TestDB.vwTestData to YOURDB.dbo.YOURTABLENAME 
$sql ="SELECT * FROM TestDB.dbo.vwTestData"; 
$data = sqlsrv_query($conn, $sql); 

$result = array(); 

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

// This will output in JSON format if you try to hit the page in a browser 
echo json_encode($result); 

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

的.js

$(document).ready(function() { 
    $('#table1').dataTable({ 
     "bProcessing": true, 
     "sAjaxSource": "scripts/script.php", 
     "sAjaxDataProp": "", 
     "aoColumns": [ 
      { "mData": "Column 1" }, 
      { "mData": "Column 2" }, 
      { "mData": "etc..." }, 
     ] 
    }); 
}); 
+0

您的服務器不解析PHP。確保php安裝在服務器上,並且您通過服務器查看頁面,而不僅僅是在瀏覽器中查看本地php文件 – Orangepill

+0

如何確保這一點?我從WebMatrix運行該網站和數據庫是在不熟悉該配置的Sql Server 2012 – Chaos

+0

,但這可能有助於http://stackoverflow.com/questions/10260691/php-not-worked-in-iis-express-當在wordpress-run-with-no-issue/10477166#10477166 – Orangepill

回答

2

我會同意,檢查服務器,以確保它在解析PHP。

此外,我發現當你訪問回聲中的php數組變量時,你必須在它們周圍放上括號。因此:

$值[主標題]

將成爲:

{$value['Primary Headings']} 

而且具有以陣列的空間作爲indicies是不好的做法,所以我會避免在未來。至於while循環,試試這個:

function associativeArrayForResult($result){ 
    $resultArray = array(); 
    for($i=0; $i<sqlsrv_num_rows($result); $i++){ 
     for($j=0; $j<sqlsrv_num_fields($result); $j++){ 
      sqlsrv_fetch($result, $i); 
      $resultArray[$i][sqlsrv_get_field($result, $j)] = sqlsrv_get_field($result, $j); 
     } 
    } 
    return $resultArray; 
} 
+1

你能告訴我如何檢查我的服務器是否解析php?我只是用WebMatrix在本地運行。 – Chaos

+0

只需編寫一個簡單的腳本,看看你的服務器是否會渲染它。例如: <?php echo「我的服務器正在渲染php :)」; ?> 將其保存到名爲test.php的文件中,並嘗試訪問它。如果你看到文本,你的服務器解析php。 –