0

I have large queries on some pages(about 30 fields on a row). I heard mysql_free_result for improve memory performance. Now I wanna ask you, How can I use properly mysql_free_result.

I did like this on my class:

    // buffered query

    private function query($queryStr)
    {
        $this->querycount++;
        $starttime = microtime();

        $this->queryRes = mysql_query($queryStr, $this->conn);
        $this->querytime = $this->querytime + (microtime() - $starttime);

        if ($this->queryRes)
        {
            $this->query_log($queryStr,$this->querytime);
        }
        else
        {
            $this->error("SQL ERROR: " . $queryStr);
        }
        return $this->queryRes;
        $this->free_result($this->queryRes);
    }

    // free result

    public function free_result($result)
    {
        mysql_free_result($result);
    }

Is this using true? If is not true, how can i use properly?

liveth19937
  • 61
  • 1
  • 2
  • 6
  • 2
    Is this old code you have to maintain? Otherwise start thinking about migrating towards MySQLi or PHP PDO. – Terence May 08 '13 at 13:29
  • 1
    [Please, don't use `mysql_*` functions](http://stackoverflow.com/q/12859942/1190388) in new code. They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the red box? Learn about prepared statements instead, and use [tag:PDO] or [tag:MySQLi]. – hjpotter92 May 08 '13 at 13:29
  • Out of interest, because the `mysql_free_result()` is being called after the method has been returned, is it actually being run? – max_ May 08 '13 at 13:30
  • 2
    There are many rubbish you can hear. Do not trust to everything you hear. Do only things you need, not things you've just heard of. – Your Common Sense May 08 '13 at 13:30
  • @YourCommonSense If you haven't heard of it, it doesn't mean you don't need to be using it. That's like using plain text to store passwords because you haven't heard of encryption. – max_ May 08 '13 at 13:32
  • Thanks guys. I think that, I will use MySQLi or PDO. – liveth19937 May 08 '13 at 13:38

2 Answers2

1

mysql_free_result() only needs to be called if you are concerned about how much memory is being used for queries that return large result sets.

All associated result memory is automatically freed at the end of the script's execution.

In your case it does not look fine, because your function is returning $this->queryRes; and it won't reach the next step.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
chandresh_cool
  • 11,753
  • 3
  • 30
  • 45
0

How can I use properly mysql_free_result.

Just don't use it.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345