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 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问