douji2283 2014-06-16 08:42
浏览 49
已采纳

无法从mssql数据库中获取结果

I have a problem whenever I query and display my resulted items. I am using PHP and MSSQL for my database. I have no problem when I query from the database but when I added an "if" statement so that to display "NO RECORDS FOUND" the problem arises.

Here's my code where I think the problem is:

//Show results in table

  if(sqlsrv_num_rows($result)>0){

    $o = '<table id="myTable">
        <thead>
        <tr>
        <th>Name</th>
        <th>Age </th>
        <th>Sex</th>
        </tr>
        </thead><tbody>';

    while ( $record = sqlsrv_fetch_array($result) )
        {

            $o .= '<tr><td>'.$record ['Name'].'</td><td>'.$record ['Age'].'</td><td>'.$record ['Sex'].'</td></tr>';
        }               

    $o .= '</tbody></table>';

    echo $o;

  }

  else{

    echo "No records found";

  }

whenever I search for a data it always display No records found even if the data is stored in the database. Help please? newbie here

  • 写回答

1条回答 默认 最新

  • doucong6884 2014-06-16 08:46
    关注

    From the documentation of sqlsrv_num_rows:

    This function requires that the statement resource be created with a static or keyset cursor.

    Make sure you create your query like this (note the SQLSRV_CURSOR_KEYSET option):

    $sql = "SELECT * FROM Table_1";
    $params = array();
    $options =  array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
    $result = sqlsrv_query( $conn, $sql , $params, $options );
    

    The reason for this, is that the default option (SQLSRV_CURSOR_FORWARD), only fetches the records from the database one row at a time. This means that the sqlsrv_num_rows function does not know the number of rows, when it is called before any rows are fetched.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起