-1

我真的很难用 MSSQL 数据库填充数据表(它在 SQL Server 2012 Express 中管理)。我已经尝试了他们网站上的几个脚本(他们的服务器端 PHP 和 ODBC 脚本);但是,没有一个有效。我偶然发现了我现在正在尝试使用的帖子( http://www.datatables.net/forums/discussion/7359/mssql-as-data-source...-im-confused/p1 )。这是我的代码

<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(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..." },
        ]
    });
});
4

1 回答 1

2

我同意,请检查您的服务器以确保它正在解析 php。

另外,我发现当你在 echo 中访问 php 数组变量时,你必须在它们周围加上括号。所以:

$value[主标题]

会成为:

{$value['Primary Headings']}

在数组中使用空格作为索引也是不好的做法,所以我以后会避免使用它。至于 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;
}
于 2013-06-03T16:13:59.770 回答