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 C#字典调用时报错,有关文件加载
  • ¥100 在连接内网VPN时,如何同时保持互联网连接
  • ¥15 MATLAB中使用parfor,矩阵Removal的有效索引在parfor循环中受限制
  • ¥20 Win 10 LTSC 1809版本如何无损提升到20H1版本
  • ¥50 win10 LTSC 虚拟键盘不弹出
  • ¥30 关于PHP中POST获取数据的问题
  • ¥30 微信小程序请求失败,网页能正常带锁访问
  • ¥15 Python实现hog特征图可视化
  • ¥30 德飞莱51单片机实现C4炸弹
  • ¥50 CrossLink-LIF-MD6000 型 FPGA 的 CMOS 转 MIPI D-PHY IP 核功能使用异常