我真的很难用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循环。 E.g。
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中构建它,我已经通过它连接了我的数据库。如果有人可以帮助我,那就太好了。感谢
.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 :(得分: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;
}