dpauxqt1281 2019-06-24 11:41
浏览 66
已采纳

从SQL Server数据库显示表

I am trying to display a table, which i can edit. My table comes up empty, even though there are records in the table. Any help would be appreciated.

code for selecting data: select.php

 <?php  
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
 ?>

<?php  
 $server = "";
$options = array( "UID" => "", "PWD" => "", "Database" => "");
global $conn;
$conn = sqlsrv_connect($server, $options);
if ($conn === false) exit("<pre>" . print_r(sqlsrv_errors(), true));

 $output = '';  
 $sql = "SELECT * FROM Table";  
 $result = sqlsrv_query($conn, $sql);  
 $output .= '  
      <div class="table-responsive">  
           <table class="table table-bordered">  
                <tr>  
                     <th width="10%">ID</th>  
                     <th width="40%">column</th>  
                     <th width="40%">column</th>  
                </tr>';  
 $rows = sqlsrv_num_rows($result);
 if($rows > 0)  
  {  
      if($rows > 10)
      {
          $delete_records = $rows - 10;
      $delete_sql = "DELETE FROM Table LIMIT $delete_records";
      sqlsrv_query($conn, $delete_sql);
  }
  while($row = sqlsrv_fetch_array($result))  
  {  
       $output .= '  
            <tr>  
                 <td>'.$row["id"].'</td>  
                 <td class="column" data-id1="'.$row["id"].'" contenteditable>'.$row["PurchaseID"].'</td>  
                 <td class="LogBookNo" data-id2="'.$row["id"].'" contenteditable>'.$row["LogBookNo"].'</td> 
<td><button type="button" name="delete_btn" data-id3="'.$row["id"].'" class="btn btn-xs btn-danger btn_delete">x</button></td>

       ';  
  }  
  $output .= '  
       <tr>  
            <td></td>  
            <td id="column1" contenteditable></td>  
            <td id="column2" contenteditable></td>
            <td><button type="button" name="btn_add" id="btn_add" class="btn btn-xs btn-success">+</button></td>  
       </tr>  
  ';  
 }  
 else  
 {  
      $output .= '
                <tr>  
                <td></td>  
                <td id="column1" contenteditable></td>  
                    <td id="column2" contenteditable></td>  
                    <td><button type="button" 
name="btn_add" id="btn_add" class="btn btn-xs btn-success">+</button></td>  
           </tr>';  
  }  
 $output .= '</table>  
      </div>';  
 echo $output;  
 ?>
  • 写回答

1条回答 默认 最新

  • duan0818 2019-06-24 13:08
    关注

    Function sqlsrv_num_rows returns the actual rows count when you use a client-side, static, or keyset cursor with sqlsrv_query. You need to use "Scrollable" => SQLSRV_CURSOR_KEYSET in your $options parameter. Another issue with your code, is that T-SQL doesn't have LIMIT keyword. In your case next answers may help.

    Next is an example, based on your code:

    <?php  
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
    ?>
    
    <?php  
    $server = "";
    $options = array( 
        "UID" => "", 
        "PWD" => "", 
        "Database" => ""
    );
    $conn = sqlsrv_connect($server, $options);
    if ($conn === false) {
        exit ("<pre>" . print_r(sqlsrv_errors(), true));
    }   
    
    $output = '';  
    $sql = "SELECT * FROM Table";  
    $result = sqlsrv_query($conn, $sql, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET));  
    $output .= '  
          <div class="table-responsive">  
               <table class="table table-bordered">  
                    <tr>  
                         <th width="10%">ID</th>  
                         <th width="40%">column</th>  
                         <th width="40%">column</th>  
                    </tr>';  
    $rows = sqlsrv_num_rows($result);
    if ($rows > 0) {  
        /* 
        if ($rows > 10) {
            $delete_records = $rows - 10;
            $delete_sql = "DELETE FROM Table LIMIT $delete_records";
            sqlsrv_query($conn, $delete_sql);
        }
        */
        while($row = sqlsrv_fetch_array($result)) {  
           $output .= '  
                <tr>  
                    <td>'.$row["id"].'</td>  
                    <td class="column" data-id1="'.$row["id"].'" contenteditable>'.$row["PurchaseID"].'</td>  
                    <td class="LogBookNo" data-id2="'.$row["id"].'" contenteditable>'.$row["LogBookNo"].'</td> 
                    <td><button type="button" name="delete_btn" data-id3="'.$row["id"].'" class="btn btn-xs btn-danger btn_delete">x</button></td>
           </tr>';  
        }  
        $output .= '  
           <tr>  
                <td></td>  
                <td id="column1" contenteditable></td>  
                <td id="column2" contenteditable></td>
                <td><button type="button" name="btn_add" id="btn_add" class="btn btn-xs btn-success">+</button></td>  
           </tr>  
      ';  
    } else {  
        $output .= 
            '<tr>  
                    <td></td>  
                    <td id="column1" contenteditable></td>  
                        <td id="column2" contenteditable></td>  
                        <td><button type="button" name="btn_add" id="btn_add" class="btn btn-xs btn-success">+</button></td>  
            </tr>';  
    }  
    
    $output .= '</table>  
          </div>';  
    echo $output;  
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊