数据库中的 MS SQL & PHP 到 return table 但它是 returning 随机字母
MS SQL & PHP to return table from database but it is returning random letters
我一直在尝试使用以下 php 代码将来自我的 MS SQL 的 table 显示到我的浏览器中,但它返回 table 并带有随机字母请参阅下面的屏幕截图和代码
这是我正在使用的代码
<?php
/* ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL); */
function databaseOutput()
{
$serverName = "*************"; //serverName\instanceName
$connectionInfo = array( "*******"=>"********", "UID"=>"******", "PWD"=>"*******!");
$db = sqlsrv_connect( $serverName, $connectionInfo);
$dbQuery = sqlsrv_query($db, "select * from vw_Impact_Return2 ");
$rows = sqlsrv_fetch_array($dbQuery);
//print('<pre>');
//print_r($rows);
//print('<pre>');
//exit;
foreach($rows as $dbRow) {
?>
<tr>
<td><?php echo $dbRow['Schema_ID']; ?></td>
<td><?php echo $dbRow['Stakeholder_ID']; ?></td>
<td><?php echo $dbRow['Intended_Changes']; ?></td>
<td><?php echo $dbRow['Investment_Type_ID']; ?></td>
<td><?php echo $dbRow['Value_of_Investment']; ?></td>
<td><?php echo $dbRow['Summary']; ?></td>
<td><?php echo $dbRow['Outcomes_Description']; ?></td>
<td><?php echo $dbRow['Outcomes_Indicator']; ?></td>
<td><?php echo $dbRow['Outcomes_Source']; ?></td>
<td><?php echo $dbRow['Outcomes_Quantity']; ?></td>
<td><?php echo $dbRow['Outcomes_Duration']; ?></td>
<td><?php echo $dbRow['Outcomes_Start']; ?></td>
<td><?php echo $dbRow['Outcomes_Financial_Proxy']; ?></td>
<td><?php echo $dbRow['Outcomes_Value_of_Proxy']; ?></td>
<td><?php echo $dbRow['Deadweight']; ?></td>
<td><?php echo $dbRow['Displacement']; ?></td>
<td><?php echo $dbRow['Attribution']; ?></td>
<td><?php echo $dbRow['Drop_Off']; ?></td>
<td><?php echo $dbRow['Impact']; ?></td>
<td><?php echo $dbRow['Return_Year0']; ?></td>
<td><?php echo $dbRow['Return_Year1']; ?></td>
<td><?php echo $dbRow['Return_Year2']; ?></td>
<td><?php echo $dbRow['Return_Year3']; ?></td>
<td><?php echo $dbRow['Return_Year4']; ?></td>
<td><?php echo $dbRow['Return_Year5']; ?></td>
</tr>
<?php
}
} // end of function databaseOutput()
if (isset($_POST['submit_docs'])) { // word output
header("Content-Type:application/msword");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("content-disposition: attachment;filename=test.docx");
?>
<html>
<body>
<h1> Social Return</h1>
<table>
<tr>
<th>Schema_ID</th><th>Stakeholder_ID</th><th>Intended_Changes</th><th>Investment_Type_ID</th><th>Value_of_Investment</th><th>Summary</th><th>Outcomes_Description</th><th>Outcomes_Indicator</th><th>Outcomes_Source</th><th>Outcomes_Quantity</th><th>Outcomes_Duration</th>
<th>Outcomes_Start</th><th>Outcomes_Financial_Proxy</th><th>Outcomes_Value_of_Proxy</th><th>Deadweight</th><th>Displacement</th><th>Attribution</th><th>Drop_Off</th><th>Impact</th>
<th>Return_Year0</th><th>Return_Year1</th><th>Return_Year2</th><th>Return_Year3</th><th>Return_Year4</th><th>Return_Year5</th>
</tr>
<?php databaseOutput(); ?>
</table>
</body>
</html>
<?php
exit; // end of word output
}
?>
<html>
<head>
<title>Social Return</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://dunluce.infc.ulst.ac.uk/cw11ba/project/Project/mycss.css">
</head>
<body>
<form name="export_form" action="<?php echo($_SERVER['PHP_SELF']);?>" method="post">
<input type="submit" name="submit_docs" value="Export as MS Word" class="input-button" /> <a href="https://dunluce.infc.ulst.ac.uk/cw11ba/project/Project/admin.php"><button type="button" class= "btn btn-block">Go back to Admin Area</button></a>
</form>
<table class="table table-striped" id="student">
<tr>
<th>Schema_ID</th><th>Stakeholder_ID</th><th>Intended_Changes</th><th>Investment_Type_ID</th><th>Value_of_Investment</th><th>Summary</th><th>Outcomes_Description</th><th>Outcomes_Indicator</th><th>Outcomes_Source</th><th>Outcomes_Quantity</th><th>Outcomes_Duration</th>
<th>Outcomes_Start</th><th>Outcomes_Financial_Proxy</th><th>Outcomes_Value_of_Proxy</th><th>Deadweight</th><th>Displacement</th><th>Attribution</th><th>Drop_Off</th><th>Impact</th>
<th>Return_Year0</th><th>Return_Year1</th><th>Return_Year2</th><th>Return_Year3</th><th>Return_Year4</th><th>Return_Year5</th>
</tr>
<?php databaseOutput(); ?>
</table>
</body>
</html>
请帮助我,我已经尝试了所有方法,但我只能通过 sqlsrv_connect
连接到我的数据库
sqlsrv_fetch_array returns 单行作为数组,因此在您的 foreach 中,您并没有循环遍历您认为的内容。尝试做一个 while 循环:
while($dbRow = sqlsrv_fetch_array($dbQuery, SQLSRV_FETCH_ASSOC)) {
?>
<tr>
<td><?php echo $dbRow['Schema_ID']; ?></td>
<td><?php echo $dbRow['Stakeholder_ID']; ?></td>
<td><?php echo $dbRow['Intended_Changes']; ?></td>
<td><?php echo $dbRow['Investment_Type_ID']; ?></td>
<td><?php echo $dbRow['Value_of_Investment']; ?></td>
<td><?php echo $dbRow['Summary']; ?></td>
<td><?php echo $dbRow['Outcomes_Description']; ?></td>
<td><?php echo $dbRow['Outcomes_Indicator']; ?></td>
<td><?php echo $dbRow['Outcomes_Source']; ?></td>
<td><?php echo $dbRow['Outcomes_Quantity']; ?></td>
<td><?php echo $dbRow['Outcomes_Duration']; ?></td>
<td><?php echo $dbRow['Outcomes_Start']; ?></td>
<td><?php echo $dbRow['Outcomes_Financial_Proxy']; ?></td>
<td><?php echo $dbRow['Outcomes_Value_of_Proxy']; ?></td>
<td><?php echo $dbRow['Deadweight']; ?></td>
<td><?php echo $dbRow['Displacement']; ?></td>
<td><?php echo $dbRow['Attribution']; ?></td>
<td><?php echo $dbRow['Drop_Off']; ?></td>
<td><?php echo $dbRow['Impact']; ?></td>
<td><?php echo $dbRow['Return_Year0']; ?></td>
<td><?php echo $dbRow['Return_Year1']; ?></td>
<td><?php echo $dbRow['Return_Year2']; ?></td>
<td><?php echo $dbRow['Return_Year3']; ?></td>
<td><?php echo $dbRow['Return_Year4']; ?></td>
<td><?php echo $dbRow['Return_Year5']; ?></td>
</tr>
<?php
}
我一直在尝试使用以下 php 代码将来自我的 MS SQL 的 table 显示到我的浏览器中,但它返回 table 并带有随机字母请参阅下面的屏幕截图和代码
这是我正在使用的代码
<?php
/* ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL); */
function databaseOutput()
{
$serverName = "*************"; //serverName\instanceName
$connectionInfo = array( "*******"=>"********", "UID"=>"******", "PWD"=>"*******!");
$db = sqlsrv_connect( $serverName, $connectionInfo);
$dbQuery = sqlsrv_query($db, "select * from vw_Impact_Return2 ");
$rows = sqlsrv_fetch_array($dbQuery);
//print('<pre>');
//print_r($rows);
//print('<pre>');
//exit;
foreach($rows as $dbRow) {
?>
<tr>
<td><?php echo $dbRow['Schema_ID']; ?></td>
<td><?php echo $dbRow['Stakeholder_ID']; ?></td>
<td><?php echo $dbRow['Intended_Changes']; ?></td>
<td><?php echo $dbRow['Investment_Type_ID']; ?></td>
<td><?php echo $dbRow['Value_of_Investment']; ?></td>
<td><?php echo $dbRow['Summary']; ?></td>
<td><?php echo $dbRow['Outcomes_Description']; ?></td>
<td><?php echo $dbRow['Outcomes_Indicator']; ?></td>
<td><?php echo $dbRow['Outcomes_Source']; ?></td>
<td><?php echo $dbRow['Outcomes_Quantity']; ?></td>
<td><?php echo $dbRow['Outcomes_Duration']; ?></td>
<td><?php echo $dbRow['Outcomes_Start']; ?></td>
<td><?php echo $dbRow['Outcomes_Financial_Proxy']; ?></td>
<td><?php echo $dbRow['Outcomes_Value_of_Proxy']; ?></td>
<td><?php echo $dbRow['Deadweight']; ?></td>
<td><?php echo $dbRow['Displacement']; ?></td>
<td><?php echo $dbRow['Attribution']; ?></td>
<td><?php echo $dbRow['Drop_Off']; ?></td>
<td><?php echo $dbRow['Impact']; ?></td>
<td><?php echo $dbRow['Return_Year0']; ?></td>
<td><?php echo $dbRow['Return_Year1']; ?></td>
<td><?php echo $dbRow['Return_Year2']; ?></td>
<td><?php echo $dbRow['Return_Year3']; ?></td>
<td><?php echo $dbRow['Return_Year4']; ?></td>
<td><?php echo $dbRow['Return_Year5']; ?></td>
</tr>
<?php
}
} // end of function databaseOutput()
if (isset($_POST['submit_docs'])) { // word output
header("Content-Type:application/msword");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("content-disposition: attachment;filename=test.docx");
?>
<html>
<body>
<h1> Social Return</h1>
<table>
<tr>
<th>Schema_ID</th><th>Stakeholder_ID</th><th>Intended_Changes</th><th>Investment_Type_ID</th><th>Value_of_Investment</th><th>Summary</th><th>Outcomes_Description</th><th>Outcomes_Indicator</th><th>Outcomes_Source</th><th>Outcomes_Quantity</th><th>Outcomes_Duration</th>
<th>Outcomes_Start</th><th>Outcomes_Financial_Proxy</th><th>Outcomes_Value_of_Proxy</th><th>Deadweight</th><th>Displacement</th><th>Attribution</th><th>Drop_Off</th><th>Impact</th>
<th>Return_Year0</th><th>Return_Year1</th><th>Return_Year2</th><th>Return_Year3</th><th>Return_Year4</th><th>Return_Year5</th>
</tr>
<?php databaseOutput(); ?>
</table>
</body>
</html>
<?php
exit; // end of word output
}
?>
<html>
<head>
<title>Social Return</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://dunluce.infc.ulst.ac.uk/cw11ba/project/Project/mycss.css">
</head>
<body>
<form name="export_form" action="<?php echo($_SERVER['PHP_SELF']);?>" method="post">
<input type="submit" name="submit_docs" value="Export as MS Word" class="input-button" /> <a href="https://dunluce.infc.ulst.ac.uk/cw11ba/project/Project/admin.php"><button type="button" class= "btn btn-block">Go back to Admin Area</button></a>
</form>
<table class="table table-striped" id="student">
<tr>
<th>Schema_ID</th><th>Stakeholder_ID</th><th>Intended_Changes</th><th>Investment_Type_ID</th><th>Value_of_Investment</th><th>Summary</th><th>Outcomes_Description</th><th>Outcomes_Indicator</th><th>Outcomes_Source</th><th>Outcomes_Quantity</th><th>Outcomes_Duration</th>
<th>Outcomes_Start</th><th>Outcomes_Financial_Proxy</th><th>Outcomes_Value_of_Proxy</th><th>Deadweight</th><th>Displacement</th><th>Attribution</th><th>Drop_Off</th><th>Impact</th>
<th>Return_Year0</th><th>Return_Year1</th><th>Return_Year2</th><th>Return_Year3</th><th>Return_Year4</th><th>Return_Year5</th>
</tr>
<?php databaseOutput(); ?>
</table>
</body>
</html>
请帮助我,我已经尝试了所有方法,但我只能通过 sqlsrv_connect
连接到我的数据库sqlsrv_fetch_array returns 单行作为数组,因此在您的 foreach 中,您并没有循环遍历您认为的内容。尝试做一个 while 循环:
while($dbRow = sqlsrv_fetch_array($dbQuery, SQLSRV_FETCH_ASSOC)) {
?>
<tr>
<td><?php echo $dbRow['Schema_ID']; ?></td>
<td><?php echo $dbRow['Stakeholder_ID']; ?></td>
<td><?php echo $dbRow['Intended_Changes']; ?></td>
<td><?php echo $dbRow['Investment_Type_ID']; ?></td>
<td><?php echo $dbRow['Value_of_Investment']; ?></td>
<td><?php echo $dbRow['Summary']; ?></td>
<td><?php echo $dbRow['Outcomes_Description']; ?></td>
<td><?php echo $dbRow['Outcomes_Indicator']; ?></td>
<td><?php echo $dbRow['Outcomes_Source']; ?></td>
<td><?php echo $dbRow['Outcomes_Quantity']; ?></td>
<td><?php echo $dbRow['Outcomes_Duration']; ?></td>
<td><?php echo $dbRow['Outcomes_Start']; ?></td>
<td><?php echo $dbRow['Outcomes_Financial_Proxy']; ?></td>
<td><?php echo $dbRow['Outcomes_Value_of_Proxy']; ?></td>
<td><?php echo $dbRow['Deadweight']; ?></td>
<td><?php echo $dbRow['Displacement']; ?></td>
<td><?php echo $dbRow['Attribution']; ?></td>
<td><?php echo $dbRow['Drop_Off']; ?></td>
<td><?php echo $dbRow['Impact']; ?></td>
<td><?php echo $dbRow['Return_Year0']; ?></td>
<td><?php echo $dbRow['Return_Year1']; ?></td>
<td><?php echo $dbRow['Return_Year2']; ?></td>
<td><?php echo $dbRow['Return_Year3']; ?></td>
<td><?php echo $dbRow['Return_Year4']; ?></td>
<td><?php echo $dbRow['Return_Year5']; ?></td>
</tr>
<?php
}