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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵