PDA

View Full Version : Difference between $vbulletin->db and $db->


zanis
10-26-2008, 01:06 PM
Hello all,

I think the code below which is within a plugin crashed my server! I was wondering if it was due to the use of $vbulletin->db and $db in the same function?

For example:

$search_sql = "SELECT threadid, title, lastpost, forumid,
replycount, dateline, postuserid, visible, open
FROM " . TABLE_PREFIX . "thread AS thread
WHERE NOT ISNULL(threadid) AND visible = '1' AND replycount > '0' AND open!='10' AND forumid IN (".$forums_to_search.") $mp_stats_timecut_activethreads
ORDER BY replycount DESC
LIMIT 0, $num_of_threads_to_display";

$active_threads_result = $vbulletin->db->query_read($search_sql);

$threadcount = $db->num_rows($active_threads_result);

# now get the results and add to an array
while ($thread_result = $db->fetch_array($active_threads_result)){
......
{

mp_stats_timecut_activethreads ends up being 1 day back in time stamp - again the code worked well and returned the correct results. The forum is also new so in total we are looking at 200 posts and 10 new ones in 10 days.

I copied the sql and code bits from a well known plugin (https://vborg.vbsupport.ru/showthread.php?t=177778) mentioned in this forum and it worked well when I was the only one calling the plugin however when I added the plugin to the forum home template and accessible to the public my server used all its memory and swap and had to be rebooted. There are two code blocks in the plugin which execute SQL for stats (the other is along the same lines).

I am unsure of why there are two access points used for the db calls ($vbulletin->db, $db) and I wonder if this is actually quite bad?

For example wouldn't you go:

while ($thread_result = $vbulletin->db->fetch_array($active_threads_result)){

instead of:

while ($thread_result = $db->fetch_array($active_threads_result)){

As I assume $vbulletin-> is actually controlling the access to the db for this instance of my code running? And I wouldn't be supprised that this in a site with say a few hundred visitors would cause issues with db sql execution etc...

Cheers

Marc

Dismounted
10-27-2008, 05:30 AM
$db =& $vbulletin->db;
:)

zanis
10-27-2008, 05:52 PM
Hello Dismounted,

Thank you for your reply.

What I failed to mention in my first post is that the code is within a function and I have declared the following:

function get_results(){
global $vbulletin,$db
...

Maybe its due to the code using two database objects that my server crashed trying to execute this code?

Cheers

Marc

Guest190829
10-27-2008, 09:01 PM
No, that code bit would not crash your server [necessarily]. We need to see the full plugin to evaluate it, there is also the possibility that the function is being run to many times causing the crash. There are a lot of possibilities.

zanis
10-27-2008, 09:25 PM
Hello Danny.VBT,

The plugin consists of code added to the plugin manager and a set of files stored outside the document root of the web site. The plugin code instantiates a class which does a few fews - one of them is to execute SQL against the forum db. Its a large class file so I have highlighted the main points below:

1. Uses pears cache_lite class - so when the plugin is called and the class loads it checks to see if the data is cached already. if so return the cache value if not continue with the data collection. I used cache_lite as I am told it has good file management (locking) etc..

2) The class returns HTML data that the plugin then echos out.

Below is the function in full that when added to the class called by the plugin the server crashed. Not sure if this did the damage but it was the last update. I also killed the cache files as well by deleting them. And the plugin code.

function mp_getMostActiveThreads(){

global $vbulletin,$db;

########## NOTES ###########
# There is no caching on getting the thread starter name and forum title via function calls fetch_userinfo() and fetch_foruminfo() however both seem to be using caching anyway.
##########################

$this->the_logger->write_log("debug","mp_getMostActiveThreads() called");

if($this->mp_isActiveThreadCache() == TRUE){
$this->the_logger->write_log("debug","mp_getMostActiveThreads:: the threads are in the caching so using that copy.");
return true;
}else{
$this->the_logger->write_log("debug","mp_getMostActiveThreads:: the threads are NOT in the cache so executing database search.");
}

$forums_to_search = "1,2,3,4,9,10,11,18,22,23,24,25,26,27,28,29";
$num_of_threads_to_display = 10;
$num_days_to_search_back = 0; # since BT is new we will go back 5 days to locate new threads
$date_format = "d-m";
$trimthreadtitle = "60"; //cut title off at 60 chars

if(!is_object($vbulletin)){
$this->the_logger->write_log("error","mp_getMostActiveThreads:: vbulletin is not an object. Terminating this functions work.");
return false;
}

if($num_days_to_search_back > 0){
$mp_stats_timecut_start = TIMENOW - ($num_days_to_search_back * 86400);
$mp_stats_timecut_activethreads = "AND dateline > $mp_stats_timecut_start";
}else{
# Note this means the entire db will be searched!!
$mp_stats_timecut_activethreads = "";
}

$search_sql = "SELECT threadid, title, lastpost, forumid, replycount, dateline, postuserid, visible, open
FROM " . TABLE_PREFIX . "thread AS thread
WHERE NOT ISNULL(threadid) AND visible = '1' AND replycount > '0' AND open!='10' AND forumid IN (".$forums_to_search.") $mp_stats_timecut_activethreads
ORDER BY replycount DESC
LIMIT 0, $num_of_threads_to_display";

$this->the_logger->write_log("debug","mp_getMostActiveThreads:: SQL to execute:: ".$search_sql);

$get_stats_hottest = $vbulletin->db->query_read($search_sql);

$threadcount = $db->num_rows($get_stats_hottest);

$this->the_logger->write_log("debug","mp_getMostActiveThreads:: Number of threads:: ".$threadcount);

# now get the results and add to an array
while ($get_hottest = $db->fetch_array($get_stats_hottest))
{
$get_hottest[fullthreadtitle] = strip_tags($get_hottest[title]);
if ($trimthreadtitle > 0)
{
$get_hottest[titletrimmed] = fetch_trimmed_title($get_hottest[fullthreadtitle], $trimthreadtitle,true);
}
else
{
$get_hottest[titletrimmed] = $get_hottest[fullthreadtitle];
}
if ($get_hottest[lastpost] > $vbulletin->userinfo['lastvisit'])
{
$get_hottest[newpost] = true;
}

$userinfo = fetch_userinfo($get_hottest[postuserid]);
$get_news_postdate = vbdate($date_format, $get_hottest[dateline]);
$foruminfo = fetch_foruminfo($get_hottest[forumid]);

$get_hottest[postusername] = $userinfo['username'];
$get_hottest[forumname] = $foruminfo['title'];
$get_hottest[threaddate] = $get_news_postdate;

$this->the_logger->write_log("debug","mp_getMostActiveThreads:: Thread trimed title:: ".$get_hottest[titletrimmed]);
$this->the_logger->write_log("debug","mp_getMostActiveThreads:: In forum:: ".$foruminfo['title']);
$this->the_logger->write_log("debug","mp_getMostActiveThreads:: Reply count:: ".$get_hottest['replycount']);

$this->mostActiveThreads[] = $get_hottest;

}//end while


$this->mp_cacheActive();
$this->the_logger->write_log("debug","mp_getMostActiveThreads:: Have cache the threads");


}


Below is the plugin code that was last used before the crash. Note that I have commented out to only have certain users trigger this. Instead the plugin executed for all forum visitors. I made it public after uploading the changes (code above) then once a saved the plugin code with comments about 20 seconds latter the server ran out of memory and had to be rebooted.


$testusers = array(1, 2, 10, 58);

//turn to true if you want to ignore caching rules
$nocache = false;

#if (in_array($vbulletin->userinfo['userid'],$testusers))
#{
$mp_cache_life_value = 3600; //1 hour
$mp_cache_life_text = "Hour";
$MMtitle = 'Information Centre - Refreshed every '.$mp_cache_life_text;

require_once '../biztalk_code/mediamanager/class.MMBuilder.php';
$builder = new MM_builder($mp_cache_life_value);

$MMinitDisplay = $builder->getDefaultStyle();
$MMinitButton = $builder->getDefaultButton();

$builder->fetch();
$builder->mp_getLatestForumNews();
$builder->mp_getMostActiveThreads();

$MMPanels = $builder->fetchPanels();

$MMjavascript = $builder->fetchJavascript();
$MMstyles = $builder->fetchStyles();

//this is at the very bottom
eval('$mediamanager = "' . fetch_template('MediaManager') . '";');
#}