PHP SQL Server无法生成错误或无法正常工作

时间:2019-03-06 17:39:21

标签: php sql-server

我是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时,结果如下

enter image description here

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

1 个答案:

答案 0 :(得分:0)

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

  1. 安装和配置PHP
  2. 安装适当的PHP扩展。根据您的代码(mssql_函数),您需要安装MSSQL扩展名(请注意,此功能在PHP 7.0.0中已删除)。另一个选择是使用PDO PHP driver for SQL Server连接到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>