Database access - is this right?

Need some coding help with a MOD or tweak you are working on? - Ask here.
Forum rules
Ask for support specifically regarding coding help with phpBB3.
Not for generic usability questions or support.

Database access - is this right?

Postby Krupski » 03 May 2012, 10:49

Hi all,

I have a text size selector on my board that saves the user's selection in the DB. In the database is a tiny unsigned INT with a default of '12" in the "phpbb_users" section:

Code: Select All
`user_view_text_size` tinyint(1) unsigned NOT NULL DEFAULT '12',


So now, please take a look at this code (the DB writing part - highlighted in red) and please tell me if I did it correctly. It works, but is it right and did I leave out any steps?

Code: Select All
function text_size()
{
global $db, $user, $template;

$name = 'pxsize'; // select name

$fs = request_var($name, 0, false, false); // check for a user selection

if ($fs) // if user selection, write it to the db
{
$sql_ary = array( 'user_view_text_size' => $fs );
$sql = 'UPDATE ' . USERS_TABLE . '
SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
WHERE user_id = ' . $user->data['user_id'];
$db->sql_query($sql);
}

else // no user selection, use current value
{
$fs = $user->data['user_view_text_size'];
}

$s_view_text_size = '<select name="' . $name . '">'; // generate the select dropdown

for ($i = 8; $i <= 24; $i++)
{
$s_view_text_size .= '<option value="' . $i . '"';
$s_view_text_size .= ($i == $fs) ? ' selected="selected"' : '';
$s_view_text_size .= '>' . $i . '</option>';
}

$s_view_text_size .= '</select>';

$template->assign_vars(array( 'S_VIEW_TEXT_SIZE' => $s_view_text_size )); // return it to the template

return $fs; // return size value to set the board body fontsize
}



Thanks!

-- Roger
''Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto.'' -- Mikhail T. Kalashnikov
User avatar
Krupski    
Lieutenant
Lieutenant
 
Posts: 287
Joined: 26 Mar 2010, 20:25
Location: PHPBB 3.0.10 with full WYSIWYG Editor. Try it with UN: "tester" and PW: "tester"
Gender: Male
phpBB Knowledge: 5




phpBB Academy at StarTrekGuide
Support STG
Using PayPal Donate

Re: Database access - is this right?

Postby Handyman » 03 May 2012, 11:44

Yes, that is correct.
I would add an int cast to the $user->data['user_id']. Even though that should be an int, I never trust anything going into the database and always either clean it (which sql_build_array does) or cast it (if it's an integer and not sent through the build array.

Code: Select all

$sql_ary 
= array(
    
'user_view_text_size'    => $fs,
);

$sql 'UPDATE ' USERS_TABLE '
    SET ' 
$db->sql_build_array('UPDATE'$sql_ary) . '
    WHERE user_id = ' 
. (int) $user->data['user_id'];
 
Please contact me if you have any news to submit to SCOFF News.
SCOFFing at the candidates while you sleep.
My Mods || My Mod Queue
Image
User avatar
Handyman    
Rear Fleet Admiral
Rear Fleet Admiral
 
Posts: 7454
Joined: 08 May 2006, 04:45
Location: Where no man has gone before!
Favorite Team: Seattle Seahawks
Gender: Male

Re: Database access - is this right?

Postby Krupski » 03 May 2012, 11:49

Handyman wrote:Yes, that is correct.
I would add an int cast to the $user->data['user_id']. Even though that should be an int, I never trust anything going into the database and always either clean it (which sql_build_array does) or cast it (if it's an integer and not sent through the build array.


I see. I'll do that. Thanks!

-- Roger
''Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto.'' -- Mikhail T. Kalashnikov
User avatar
Krupski    
Lieutenant
Lieutenant
 
Posts: 287
Joined: 26 Mar 2010, 20:25
Location: PHPBB 3.0.10 with full WYSIWYG Editor. Try it with UN: "tester" and PW: "tester"
Gender: Male
phpBB Knowledge: 5

Re: Database access - is this right?

Postby Krupski » 03 May 2012, 14:36

Handyman wrote:Yes, that is correct.


Let me ask you one more thing:

I see code like this:

Code: Select all
    $result = $db->sql_query($sql);
    ....
    ....
    ....
    $db->sql_freeresult($result);


While other times it's just:

Code: Select all
    $db->sql_query($sql);


What is the purpose of "freeresult($result)"?

Thanks.

-- Roger
''Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto.'' -- Mikhail T. Kalashnikov
User avatar
Krupski    
Lieutenant
Lieutenant
 
Posts: 287
Joined: 26 Mar 2010, 20:25
Location: PHPBB 3.0.10 with full WYSIWYG Editor. Try it with UN: "tester" and PW: "tester"
Gender: Male
phpBB Knowledge: 5

Re: Database access - is this right?

Postby topdown » 03 May 2012, 23:34

$db->sql_freeresult() should always be run after you are done with the query results.

It frees the memory that was holding the query results.
Its the same as PHP's http://php.net/manual/en/function.mysql-free-result.php
Do not PM me for Support unless I give permission in a post......PM's only help one, posts help everyone !
User avatar
topdown    
STG Styles Leader
STG Styles Leader
 
Posts: 3022
Joined: 01 Oct 2007, 22:56
Location: Handyman's harddrive
Favorite Team: STG Teams
Gender: Male
phpBB Knowledge: 9

Re: Database access - is this right?

Postby Krupski » 04 May 2012, 06:31

topdown wrote:$db->sql_freeresult() should always be run after you are done with the query results.

It frees the memory that was holding the query results.
Its the same as PHP's http://php.net/manual/en/function.mysql-free-result.php



So then my code *should* be like this:

Code: Select all

            $sql_ary 
= array( 'user_view_text_size' => $fs );
            $sql = 'UPDATE ' . USERS_TABLE . '
            SET '
 . $db->sql_build_array('UPDATE', $sql_ary) . '
            WHERE user_id = '
 . (INT) $user->data['user_id'];
            $result = $db->sql_query($sql);
            $db->sql_freeresult($result);
 



Correct?
''Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto.'' -- Mikhail T. Kalashnikov
User avatar
Krupski    
Lieutenant
Lieutenant
 
Posts: 287
Joined: 26 Mar 2010, 20:25
Location: PHPBB 3.0.10 with full WYSIWYG Editor. Try it with UN: "tester" and PW: "tester"
Gender: Male
phpBB Knowledge: 5

Re: Database access - is this right?

Postby Handyman » 04 May 2012, 10:56

You don't need the freeresult for an update statement. It's for select statements to free the memory from query results.
Please contact me if you have any news to submit to SCOFF News.
SCOFFing at the candidates while you sleep.
My Mods || My Mod Queue
Image
User avatar
Handyman    
Rear Fleet Admiral
Rear Fleet Admiral
 
Posts: 7454
Joined: 08 May 2006, 04:45
Location: Where no man has gone before!
Favorite Team: Seattle Seahawks
Gender: Male

Re: Database access - is this right?

Postby Krupski » 04 May 2012, 12:55

Handyman wrote:You don't need the freeresult for an update statement. It's for select statements to free the memory from query results.


OK I get it now. There is nothing TO free with an update statement.

Thank you. :good:
''Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto.'' -- Mikhail T. Kalashnikov
User avatar
Krupski    
Lieutenant
Lieutenant
 
Posts: 287
Joined: 26 Mar 2010, 20:25
Location: PHPBB 3.0.10 with full WYSIWYG Editor. Try it with UN: "tester" and PW: "tester"
Gender: Male
phpBB Knowledge: 5


Return to phpBB3 Coding Assistance

Who is online

Users browsing this forum: No registered users and 11 guests