【问题标题】:Filling Datatables with MSSQL data [duplicate]用 MSSQL 数据填充数据表 [重复]
【发布时间】:2013-06-03 14:51:24
【问题描述】:

我真的很难用 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..." },
        ]
    });
});

【问题讨论】:

  • 您的服务器没有解析 PHP。确保服务器上安装了 php,并且您正在通过所述服务器查看页面,而不仅仅是在浏览器中查看本地 php 文件
  • 我如何确保这一点?我从 WebMatrix 运行网站,数据库在 Sql Server 2012 中
  • 不熟悉该配置,但这可能会有所帮助stackoverflow.com/questions/10260691/…
  • 在WebMatrix的设置中,Enable PHP已经开启

标签: php sql-server datatables webmatrix


【解决方案1】:

我同意,请检查您的服务器以确保它正在解析 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;
}

【讨论】:

  • 你能告诉我如何检查我的服务器是否正在解析 php 吗?我只是在本地使用 WebMatrix 运行它。
  • 只要写一个简单的脚本,看看你的服务器是否会渲染它。例如: 将其保存到名为 test.php 的文件中并尝试访问它。如果你看到文本,你的服务器正在解析 php。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-05
  • 2013-10-26
  • 2013-04-13
  • 2013-11-01
  • 1970-01-01
相关资源
最近更新 更多