【问题标题】:all retrieve data from ms accesss database in php所有从 php 中的 ms access 数据库中检索数据
【发布时间】:2023-04-08 15:13:01
【问题描述】:

使用 odbc 驱动程序从 php 中的 ms access 数据库 2007 中检索数据。使用查询检索所有数据,但它只获取一条记录检索其他数据不检索。

下面查询了三条记录,但它只检索到一条数据。 php中代码下面的哪个问题?如何使用此代码中的查询获取所有数据有什么变化?

 <?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
    }

    ?>

【问题讨论】:

    标签: php odbc ms-access-2007


    【解决方案1】:

    您只返回 JSON 数据中的一条记录,因为您没有遍历记录集。最初我误读了您在同一个记录集上调用了两次odbc_fetch_array,但仔细检查后发现,据我所知,暗示使用了一个查询来查看是否有可能从主查询返回的任何记录。下面重写的代码尚未经过测试-我没有办法这样做-并且只有一个查询,但确实尝试遍历循环。

    如果出于某种原因需要记录数,我将 count 作为子查询包含在主查询中 - 但我不认为是这样。

    <?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;
    ?>
    

    【讨论】:

    • PHP 警告:odbc_num_rows():提供的资源不是第 18 行 C:\Project\Year2.php 中的有效 ODBC 结果资源
    • 啊 - 认为应该是$rows = odbc_num_rows( $stmt );
    猜你喜欢
    • 2023-04-04
    • 2017-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-16
    • 1970-01-01
    • 1970-01-01
    • 2022-09-29
    相关资源
    最近更新 更多