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 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记