douxiawei9318 2019-04-06 07:00
浏览 66
已采纳

所有从PHP中的ms访问数据库中检索数据

Data retrieve from the ms access database 2007 in php using odbc driver. ALL data retrieve using query but its get only one record retrieve other data is not retrieve.

below query three records but its retrieved only one data. which problem below code in php?how get all data using query from this code what's changes it?

 <?PHP

    include 'Connection2.php';



    $sql = "select FYearID,Description,FromDate,ToDate  from mstFinancialyear";


    $stmt = odbc_exec($conn, $sql);
    //print_r($stmt);
    $rs = odbc_exec($conn, "SELECT Count(*) AS counter from mstFinancialyear");

    //print_r($stmt);

    $arr = odbc_fetch_array($rs);
    $arr1 = $arr['counter'];
    $result = array(); 

    //print_r($arr);


     if (!empty($stmt)) {

            // check for empty result
            if ($arr1 > 0) {
    // print_r($stmt);

                $stmt1 = odbc_fetch_array($stmt);




               $year = array();
                $year['FYearID'] = $stmt1['FYearID'];
                $year['Description'] = $stmt1['Description'];
                $year['FromDate'] = $stmt1['FromDate'];
                $year['ToDate'] = $stmt1['ToDate'];


                // success
                $result["success"] = 1;

                // user node
                $result["year"] = array();


                array_push($result["year"], $year); 

                echo json_encode($result);

                //return true;

            } else {
                // no product found
                $result["success"] = 0;
                $result["message"] = "No product found";




                echo json_encode($result);


            }


            odbc_close($conn); //Close the connnection first
    }

    ?>
  • 写回答

1条回答 默认 最新

  • dongtiandexue123456 2019-04-06 07:52
    关注

    You return only a single record in the JSON data because you do not iterate through the recordset. Initially I misread that you had called odbc_fetch_array twice on the same recordset but upon closer inspection see that one query is imply used, as far as I can tell, to see if there are any records likely to be returned from the main query. The re-written code below has not been tested - I have no means to do so - and has a single query only but does attempt to iterate through the loop.

    I included the count as a sub-query in the main query if for some reason the number of records was required somehow - I don't think that it is however.

    <?php
    
        include 'Connection2.php';
    
        $result=array();
    
        $sql = "select 
                ( select count(*) from `mstFinancialyear` ) as `counter`,
                `FYearID`, 
                `Description`,
                `FromDate`,
                `ToDate` 
            from 
            `mstFinancialyear`";
    
        $stmt = odbc_exec( $conn, $sql );
    
        $rows = odbc_num_rows( $conn );
        /* odbc_num_rows() after a SELECT will return -1 with many drivers!! */
    
    
        /* assume success as `odbc_num_rows` cannot be relied upon */
        if( !empty( $stmt ) ) {
    
            $result["success"] = $rows > 0 ? 1 : 0;
            $result["year"] = array();
    
            /* loop through the recordset, add new record to `$result` for each row/year */
            while( $row=odbc_fetch_array( $stmt ) ){ 
    
                $year = array();
                $year['FYearID'] = $row['FYearID'];
                $year['Description'] = $row['Description'];
                $year['FromDate'] = $row['FromDate'];
                $year['ToDate'] = $row['ToDate'];
    
                $result["year"][] = $year;
    
            }
            odbc_close( $conn );
        }
    
        $json=json_encode( $result );
        echo $json;
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加