I am trying to display the result from a query in to a table. I can do this pretty easily when doing queries from mysql, but cannot get it to work with mssql. Below is the code I used to actually make sure I could get the data (and it works fine). I just cannot figure out how to get it to display in a table. If anyone could point me in the right direction I would appreciate it.

<?php
$myServer = "server";
$myUser = "user";
$myPass = "password";
$myDB = "db"; 

//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 col1, col2 ";
$query .= "FROM sqltable ";

//Execute the SQL query and return records
$result = mssql_query($query)
	or die('A error occured: ' . mysql_error());

//Show result
while ( $record = mssql_fetch_array($result) )
{
	echo $record["col1"] . " , " . $record["col2"] . "<br />";
}

//Free result set memory
mssql_free_result($result);

//Close the connection
mssql_close($dbhandle);
?>

I am getting closer. My issue now is getting the table to display the values from the actual mssql table. See code below:

<html>
    <head>
        <title>Page Title</title>
    </head>
    <body>
    		
        <?php
            $myServer = "server";
            $myUser = "user";
            $myPass = "password";
            $myDB = "mssqldb"; 

        //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 col1, col2 ";
            $query .= "FROM sqltable ";

        //execute the SQL query and return records
            $result = mssql_query($query)
                or die('A error occured: ' . mysql_error());

        //Show results in table

	    $o = '<table id="myTable">
                <thead>
                <tr>
                <th>Col 1</th>
                <th>Col 2</th>
                </tr>
                </thead><tbody>';
                
               while ( $record = mssql_fetch_array($result) )
                {
                    $o .= '<tr><td>'.$col1.'</td><td>'.$col2.'</td></tr>';
                }               
                
                $o .= '</tbody></table>';
                
                echo $o;
                
        //Show result from table separated by comma (commented out)
           /* while ( $record = mssql_fetch_array($result) )
            {
                echo $record["col1"] . " , " . $record["col2"] . "<br />";
            } */

        //free result set memory
            mssql_free_result($result);

        //close the connection
            mssql_close($dbhandle);
        ?>
    </body>
</html>

Ok ... got my answer on stackoverflow. I did not define my variables. Here is the code I actually needed

while ( $record = mssql_fetch_array($result) )
          {
              $o .= '<tr><td>'.$record ['col1'].'</td><td>'.$record ['col2'].'</td></tr>';
          }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.