1,105,406 Community Members

Display MSSQL Query in Table using PHP

Member Avatar
klcant
Newbie Poster
3 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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);
?>
Member Avatar
klcant
Newbie Poster
3 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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>
Member Avatar
klcant
Newbie Poster
3 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
1
 

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>';
          }
Question Self-Answered as of 2 Years Ago
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: