News:

Wondering if this will always be free?  See why free is better.

Main Menu

db_free_result Question

Started by The Wizard, November 16, 2012, 10:44:33 AM

Previous topic - Next topic

The Wizard

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

All Colours Sam

You must place it right before returning your value, that is because php doesn't execute anything below or after a return sentence.
Oh, wouldn't it be great if I *was* crazy? ...then the world would be okay
Suki

The Wizard

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 />';

}


All Colours Sam

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.
Oh, wouldn't it be great if I *was* crazy? ...then the world would be okay
Suki

The Wizard

@Suki - Thank you so much for the help, explanation, and suggestions.

Wiz

Advertisement: