dongyun7571 2013-07-27 20:02
浏览 37
已采纳

mysqli_free_results的问题

I'm having trouble using the mysqli_free_result() function. I am nesting queries and it seems when I free the results it stops the initial query from running its loop with the while() function. Am I using the mysqli_free_result() improperly? If I change the name of the 2nd query from $results to $results2 it works fine. Seems there is an issue with the variables over writting, which is where I thought mysqli_free_result() would help but apparently not.

Here is a sample code similar to what I am trying to do.

<?php

// 1st query
$query = '';

// Run the 1st query
if( $results = mysqli_query( $db_connect, $query ) ) {
    while( $result = mysqli_fetch_assoc( $results ) ) {

        // 2nd query
        $query = '';

        // Run the 2nd query
        if( $results = mysqli_query( $db_connect, $query ) ) {
            while( $result = mysqli_fetch_assoc( $results ) ) {

            }
            mysqli_free_result( $results );
        }

    }
    mysqli_free_result( $results );
}

?>
  • 写回答

2条回答 默认 最新

  • doutao1939 2013-07-27 20:06
    关注

    Try using a different variable name for your inner query. Currently you are overwriting the $results on every run of the outer while loop.

    <?php
    
    // 1st query
    $query = '';
    
    // Run the 1st query
    if( $results = mysqli_query( $db_connect, $query ) ) {
        while( $result = mysqli_fetch_assoc( $results ) ) {
    
            // 2nd query
            $query = '';
    
            // Run the 2nd query
            if( $results_2 = mysqli_query( $db_connect, $query ) ) {
                while( $result_2 = mysqli_fetch_assoc( $results_2 ) ) {
    
                }
                mysqli_free_result( $results_2 );
            }
    
        }
        mysqli_free_result( $results );
    }
    
    ?>
    

    Update: As already pointed out on other comments and answers, mysqli_stmt_free_result frees the memory for the supplied statement handle as stated in the docs http://www.php.net/manual/en/mysqli-stmt.free-result.php. As @andrewsi points out, the issue is variable scoping issue.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!