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条)

报告相同问题?

悬赏问题

  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案