0

我是 PHP 新手,所以我为我的无知道歉。我试图将 PHP 连接到我计算机上的 SQL Server 数据库。我希望表格显示在网页上,所以这是早期的测试和学习。我的代码是

<!DOCTYPE html>
<html>
<head>
    <title>Test</title>
</head>
<body>
<table border="5" bgcolor="white" width="300" align="center">
<tr>
<th bgcolor="grey">InputType</th>
<th bgcolor="">Valid</th>
</tr>
<?php
$myServer = "localhost";
$myUser = "xxxxx";
$myPass = "xxxx";
$myDB = "xxxxxxxxx"; 
//Connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer"); 
//Select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
or die("Couldn't open database $myDB"); 
//Declare the SQL statement that will query the database
$query = "SELECT inputtype, valid FROM dbo.eqinputs";
//Execute the SQL query and return records
$result = mssql_query($query)
or die('A error occured: ' . mysql_error());
//Show result
while ( $rows = mssql_fetch_array($result) )
      {
    $InputType=$rows['InputType'];
    $Valid=$rows['Valid'];
echo "<tr> 
    <td>$InputType</td>
    <td>$Valid</td>
  </tr>";
      }
?>
</table>
</body>
</html>

当我这样做并保存为 .php 时,结果如下

在此处输入图像描述

如您所见,代码本身以及与我的数据库的连接都存在错误。任何帮助将不胜感激

4

1 回答 1

0

您的 PHP 文件似乎被解析为 HTML。您需要执行以下操作:

  1. 安装和配置PHP
  2. 安装适当的 PHP 扩展。根据您的代码(mssql_函数),您需要安装MSSQL扩展(请注意,此功能在 PHP 7.0.0 中已删除)。另一种选择是使用 SQL Server 的PDO PHP 驱动程序连接到 SQL Server 。

带有 MS SQL 扩展的示例:

<!DOCTYPE html>
<html>
<head>
    <title>Test</title>
</head>
<body>
    <table border="5" bgcolor="white" width="300" align="center">
    <tr>
    <th bgcolor="grey">InputType</th>
    <th bgcolor="">Valid</th>
    </tr>

<?php
$myServer = "localhost";
$myUser   = "xxxxx";
$myPass   = "xxxx";
$myDB     = "xxxxxxxxx"; 

// Connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass) or die("Couldn't connect to SQL Server on $myServer"); 
$selected = mssql_select_db($myDB, $dbhandle) or die("Couldn't open database $myDB"); 

// Declare the SQL statement that will query the database
$query = "SELECT inputtype, valid FROM dbo.eqinputs";
$result = mssql_query($query) or die('Error message: ' . mssql_get_last_message());

//Show result
while ($rows = mssql_fetch_array($result)) {
    $InputType = $rows['InputType'];
    $Valid = $rows['Valid'];
    echo "<tr><td>".$InputType."</td><td>".$Valid."</td></tr>";
}
?>
    </table>
</body>
</html>

SQL Server 的 PDO PHP 驱动程序示例:

<!DOCTYPE html>
<html>
<head>
    <title>Test</title>
</head>
<body>
    <table border="5" bgcolor="white" width="300" align="center">
    <tr>
    <th bgcolor="grey">InputType</th>
    <th bgcolor="">Valid</th>
    </tr>
<?php
# Connection info
$myServer = "localhost";
$myUser   = "xxxxx";
$myPass   = "xxxx";
$myDB     = "xxxxxxxxx"; 

# Connection
try {
    $dbh = new PDO("sqlsrv:server=$myServer;Database=$myDB", $myUser, $myPass);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch( PDOException $e ) {
    die( "Error connecting to SQL Server. ".$e->getMessage());
}

# Statement
try {
    $sql = "SELECT inputtype, valid FROM dbo.eqinputs";
    $stmt = $dbh->query($sql);
    while ($row = $stmt->fetch( PDO::FETCH_ASSOC )) {
        echo "<tr><td>".$row['InputType']."</td><td>".$row['Valid']."</td></tr>";
    }
    echo "<br>";
} catch( PDOException $e ) {
    die( "Error executing stored procedure: ".$e->getMessage());
}
$stmt = null;

# End
$dbh = null;
?>
    </table>
</body>
</html>
于 2019-03-07T09:16:42.677 回答