Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: Sir Osis of Liver on February 08, 2015, 12:22:56 AM

Title: Query no worky
Post by: Sir Osis of Liver on February 08, 2015, 12:22:56 AM
(https://www.simplemachines.org/community/proxy.php?request=http%3A%2F%2Fwww.thekrashsite.com%2Fpics%2Fhairpull1.gif&hash=d1684c7b73ef4c7cc773994cfce0795f18f1c1d9) Trying to add a query to function setupMenuContext()
in Subs.php, but any query I use, including legit queries copied from earlier in the file, cause white screen.  What am I missing?
Title: Re: Query no worky
Post by: live627 on February 08, 2015, 12:50:12 AM
QuoteWhat am I missing?
the code :P
Title: Re: Query no worky
Post by: Arantor on February 08, 2015, 07:06:30 AM
Do you really need to run the query every single page, even for guests?
Title: Re: Query no worky
Post by: Sir Osis of Liver on February 08, 2015, 03:25:27 PM
I want to light up the 'Gallery' menu button on main menu when a new image has been posted within the past 24 hours, using something like this -



SELECT MAX(last_pic_date) FROM {db_prefix}gallery_cat



... and then this -



'title' => '<span style="color: '.$color.';"> Gallery </span>',



Overhead is not a concern, this is my family board and not very busy.  I've done something similar in the gallery source code to light up the gallery names and comment totals when new items are posted, but that only affects the gallery template.

Title: Re: Query no worky
Post by: Arantor on February 08, 2015, 05:02:54 PM
So you have a query that you haven't provided, being spliced into some part of a file, which we don't know how you're trying to do it...?

Having established the viability of premise, if you could provide the code that isn't working, maybe we'll be able to help you?
Title: Re: Query no worky
Post by: Sir Osis of Liver on February 09, 2015, 12:59:30 PM
Didn't save the code, no time to redo it now, but the basic problem is that any query I insert here -



/// All the buttons we can possible want and then some, try pulling the final list of buttons from cache first.

>>>>>>>>>

if (($menu_buttons = cache_get_data('menu_buttons-' . implode('_', $user_info['groups']) . '-' . $user_info['language'], $cacheTime)) === null || time() - $cacheTime <= $modSettings['settings_updated'])



causes a blank screen.  Will put something more intelligible together if I have time tonight.

Title: Re: Query no worky
Post by: NanoSector on February 09, 2015, 01:07:41 PM
White screens can occur if you have a parse error and do not have display_errors enabled in php.ini, check that first.
Title: Re: Query no worky
Post by: Sir Osis of Liver on February 09, 2015, 01:18:11 PM
Not a parse error, they give me internal server errors.  Copied a query from further up in Subs.php and got same result.  Have to toddle off, will tinker with it tonight.
Title: Re: Query no worky
Post by: Arantor on February 09, 2015, 05:02:31 PM
You realise that if you post the *actual* code you're trying to use, you might get somewhere.
Title: Re: Query no worky
Post by: Sir Osis of Liver on February 09, 2015, 10:49:17 PM
This works -



/// All the buttons we can possible want and then some, try pulling the final list of buttons from cache first.

$result = mysql_query("SELECT MAX(poster_time) FROM smf_messages");
$row = mysql_fetch_array($result);
$last_post = $row['MAX(poster_time)'];

echo '<br />$last_post = ' . $last_post;
exit;

if (($menu_buttons = cache_get_data('menu_buttons-' . implode('_', $user_info['groups']) . '-' . $user_info['language'], $cacheTime)) === null || time() - $cacheTime <= $modSettings['settings_updated'])



... but anything I do with $smcFunc['db_query'] causes white screen.
Title: Re: Query no worky
Post by: vbgamer45 on February 09, 2015, 10:58:07 PM
Is $smcFunc in the globals of the function?
Title: Re: Query no worky
Post by: Sir Osis of Liver on February 09, 2015, 11:24:09 PM
(https://www.simplemachines.org/community/proxy.php?request=http%3A%2F%2Fwww.thekrashsite.com%2Fpics%2Ffp.gif&hash=d0889cee6120d0a650f84d729e497abcf6c7e975) No, it's not.  Knew I was missing something obvious.  Will give it a try soon as I figure out why my math function is working backwards.  Thx.
Title: Re: Query no worky
Post by: Sir Osis of Liver on February 10, 2015, 12:09:08 AM
Ok, this works -



/// All the buttons we can possible want and then some, try pulling the final list of buttons from cache first.

$result = $smcFunc['db_query']('','
SELECT MAX(last_pic_date)
FROM {db_prefix}gallery_cat');

$row = mysql_fetch_array($result);
$last_pic_date = $row['MAX(last_pic_date)'];

if ($last_pic_date > (time() - 86400))
$color = '#FF8300';



It lights up the menu button with this -



// [SMF Gallery] button
'gallery' => array(

'title' => '<span style="color: '.$color.';">'. $txt['smfgallery_menu'] . '</span>',
'href' => $scripturl . '?action=gallery',
'show' => $context['allow_smfgallery_view'],
'icon' => '',
),



What's the difference between using $smcFunc['db_query'] and mysql_query?
Title: Re: Query no worky
Post by: Arantor on February 10, 2015, 12:51:07 AM
$smcFunc contains proper methods for safe querying  unlike mysql_query. Also, when PHP removes the mysql_query function (scheduled after 5.5), it is possible to cleanly replace everything in $smcFunc.
Title: Re: Query no worky
Post by: NanoSector on February 10, 2015, 06:51:04 AM
mysql_fetch_array

You should probably be using the functions inside $smcfunc here too.