Call mysql_free_result on which var?

Discussion in 'PHP' started by Tony Brar, Nov 21, 2012.

  1. #1
    Hi everyone,

    I have this piece of code:
    
    $namelist = mysql_query("SELECT username FROM users") or die('Error:' . mysql_error() . '<br />Error number:' . mysql_errno());
    $namearray = mysql_fetch_array($namelist);
    
    PHP:
    I want to use mysql_free_result to save memory.
    Would I call it on $namelist, $namearray, or both?
    Also, could I just use unset() on those variables?

    Thanks,
    -Tony
     
    Tony Brar, Nov 21, 2012 IP
  2. Anonimista

    Anonimista Peon

    Messages:
    12
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    3
    #2
    You would call it on the resource returned by mysql_query(), in your case $namelist. Note that Mysql PHP extension usage is discouraged.
     
    Anonimista, Nov 21, 2012 IP
  3. Tony Brar

    Tony Brar Active Member

    Messages:
    220
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    75
    #3
    Okay.
    Could I call unset() on $namearray, to free more memory?
    Also, I know about the mysql php extension being discouraged. I'm still learning PDO.

    Thanks,
    -Tony
     
    Tony Brar, Nov 21, 2012 IP
  4. Anonimista

    Anonimista Peon

    Messages:
    12
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    3
    #4
    unset() removes the variable but it does not free the underlying resource. $namearray is an array, so yes. Didn't mean to be condescending with the Mysql remark, many people use it because they find online examples and don't bother to read the manual
     
    Anonimista, Nov 21, 2012 IP
  5. Tony Brar

    Tony Brar Active Member

    Messages:
    220
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    75
    #5
    So what you're saying is, use mysql_free_result() on $namelist, and unset() on $namearray?

    -Tony
     
    Tony Brar, Nov 21, 2012 IP
  6. Anonimista

    Anonimista Peon

    Messages:
    12
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    3
    #6
    Why don't you use memory_get_usage() to check mysql_free_result() and unset() effect on your script memory usage?
     
    Anonimista, Nov 21, 2012 IP
  7. Tony Brar

    Tony Brar Active Member

    Messages:
    220
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    75
    #7
    Good idea.
    But now, I just started my website. I have one user (me!).
    I'm just thinking for the future...

    -Tony
     
    Tony Brar, Nov 21, 2012 IP
  8. DomainerHelper

    DomainerHelper Well-Known Member

    Messages:
    445
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    100
    #8
    You're using a lot of resources to clear the memory. Shooting yourself in the foot. Offset it a bit by using mysqli
     
    DomainerHelper, Nov 21, 2012 IP