Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: The Wizard on November 16, 2012, 10:44:33 AM

Title: db_free_result Question
Post by: The Wizard on November 16, 2012, 10:44:33 AM
Hello:

I'm working on my mod, and the Customization Team has sent me a list of changes I need to make.
One of those changes is using $smcFunc['db_free_result'](result)
I understand why they want me to use it, and my question is did I put it in the correct place or not. I think I correctly added it, but it never hurts to ask and make sure right?

Below is the code in question and where I have added it -


function onUse()
{
global $smcFunc, $user_info;

// Get the user's information from the database

      $result = $smcFunc['db_query']('', "
      SELECT wizard
     FROM {db_prefix}members
     WHERE id_member = {int:current_member}
     LIMIT 1",
     array(
       'current_member' => $user_info['id'],
     ));

     // fetch their information

$row = $smcFunc['db_fetch_assoc']($result);

// add your value to the array - in this case its buzz

    $B = $row['wizard'] . (!empty($row['wizard']) ? ',' : '') . 'buzz';

// Save all the orginal image names plus the new value

     updateMemberData($user_info['id'], array('wizard' => $B));

     return '<br><h2>To infinity and beyond!!<br /><br />You now own Buzz Lightyear</h2><br />';
     
// Free the result

             $smcFunc['db_free_result']($result);

}


Thanks

Wiz
Title: Re: db_free_result Question
Post by: All Colours Sam on November 16, 2012, 11:14:07 AM
You must place it right before returning your value, that is because php doesn't execute anything below or after a return sentence.
Title: Re: db_free_result Question
Post by: The Wizard on November 16, 2012, 11:18:46 AM
my bad should have seen that myself. So like this -


function onUse()
{
global $smcFunc, $user_info;

// Get the user's information from the database

      $result = $smcFunc['db_query']('', "
      SELECT wizard
     FROM {db_prefix}members
     WHERE id_member = {int:current_member}
     LIMIT 1",
     array(
       'current_member' => $user_info['id'],
     ));

     // fetch their information

$row = $smcFunc['db_fetch_assoc']($result);

// add your value to the array - in this case its buzz

    $B = $row['wizard'] . (!empty($row['wizard']) ? ',' : '') . 'buzz';

// Save all the orginal image names plus the new value

     updateMemberData($user_info['id'], array('wizard' => $B));

// Free the result

             $smcFunc['db_free_result']($result);

     return '<br><h2>To infinity and beyond!!<br /><br />You now own Buzz Lightyear</h2><br />';

}

Title: Re: db_free_result Question
Post by: All Colours Sam on November 16, 2012, 11:24:41 AM
Yeah, or you could also using it right after you got the data you need from the DB, that is up to you.

you can also use return and its behaviour to your own favor, for example, if the query got no data form the DB, you can use

return false;

to terminate the function before anything else gets executed, then when you use your function you can check for the returning value, if it isn't false, then you are completely sure you got something from the DB.
Title: Re: db_free_result Question
Post by: The Wizard on November 16, 2012, 11:30:20 AM
@Suki - Thank you so much for the help, explanation, and suggestions.

Wiz