从SQL Server数据库显示表

时间:2019-06-24 11:41:54

标签: php html sql-server

我正在尝试显示一个可以编辑的表格。即使表中有记录,我的表还是空的。任何帮助将不胜感激。

用于选择数据的代码: 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 个答案:

答案 0 :(得分:0)

函数sqlsrv_num_rows返回实际的行数,当您将客户端,静态或键集游标与sqlsrv_query一起使用时。您需要在"Scrollable" => SQLSRV_CURSOR_KEYSET参数中使用$options。您的代码的另一个问题是T-SQL没有LIMIT关键字。对于您而言,下一个answers可能会有所帮助。

接下来是一个基于您的代码的示例:

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