简体   繁体   English

在php表中显示MSSQL选择数据

[英]Display MSSQL select data in php table

I am missing something simple I am sure. 我很遗憾,我错过了一些简单的事情。 This is the first time I am pulling data from a MSSQL server and displaying it in a table using php. 这是我第一次从MSSQL服务器提取数据并使用php在表中显示它。 I have done this in the past with mysql, but cannot get it to work with mssql. 我以前用mysql做过这个,但是不能让它与mssql一起工作。 Here is my current code: 这是我目前的代码:

<?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 sql 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);
    ?>

$col1 and $col2 variables are never defined. 永远不会定义$ col1和$ col2变量。 You should use what is in the commented section, $record["colname"]. 您应该使用注释部分$ record [“colname”]中的内容。

while ( $record = mssql_fetch_array($result) )
          {
              $o .= '<tr><td>'.$record ['col1'].'</td><td>'.$record ['col2'].'</td></tr>';
          }      

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM