0

I am new to PHP, so I apologize for the ignorance. I have tried to connect PHP to my SQL Server database on my computer. I would like for the table to be displayed on a webpage, so this is early testing and learning. My code is

<!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>

When I do this and save as a .php, here is the result

enter image description here

As you can see, there is an error on the code itself, as well as the connection to my database. Any assistance would be greatly appreciated

Zhorov
  • 28,486
  • 6
  • 27
  • 52
baskinsr
  • 61
  • 2
  • 16
  • 1
    `mysql_error()` will not work with SQL Server. The mssql_ library has its own error reporting functionality – ADyson Mar 06 '19 at 17:45
  • anyway, the state of your page in the screenshot suggests that perhaps the PHP script is not actually being executed, and the browser is trying to parse it as HTML instead. Check you've actually installed PHP properly and integrated it with your webserver (you can google the instructions for your specific O/S and choice of webserver), and that you've placed the .php file in a valid location within the webserver's root folder. And obviously you need to serve the page over `http://` not `file:///` or anything like that – ADyson Mar 06 '19 at 17:48

1 Answers1

0

It seems that your PHP file is parsed as HTML. You need to do the following:

  1. Install and configure PHP
  2. Install appropriate PHP extension. Based on your code (mssql_ functions), you need to install MSSQL extension (note that this feature was REMOVED in PHP 7.0.0). Another option is to connect to SQL Server with PDO PHP driver for SQL Server.

Example with MS SQL extension:

<!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>

Example with PDO PHP Driver for 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
# 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>
Zhorov
  • 28,486
  • 6
  • 27
  • 52