PDA

View Full Version : Using $vbulletin or $db in non-vb powered php file?


saajjj
01-01-2010, 11:43 AM
Hello,
This post seems to have grown. I wanted to explain exactly what I was doing so that someone might point me in the right direction.

I've made a custom template inside which I want to output from a PHP file.
This customTemplate is itself used inside the header template. The plugin for this customTemplate looks like:
(hook: parse_templates)
$templater = vB_Template::create('customTemplate');
$templatevalues['customTemplate_show'] = $templater->render();
vB_Template::preRegister('header', $templatevalues);

The plugin I'm using to enable me to write from the PHP file is:
(hook: parse_templates)
ob_start();
include("http://www.myforum.com/myPHPfile.php");
$contents = ob_get_contents();
ob_end_clean();
$preRegister['phpFile_output'] = $contents;
vB_Template::preRegister('customTemplate', $preRegister);

So, the forum header has {vb:raw customTemplate_show} and customTemplate has {vb:raw phpFile_output}

The PHP file is simply:
<?php
echo("hello world");
?>

The above works as expected. i.e in the header I can see 'hello world'.

My problem starts when I want to start using vB specific globals in myPHPFile.php like $vbulletin or $db. I get a:
Fatal error: Call to a member function query_read() on a non-object in /home/xxxx/public_html/myPHPFile.php on line 9

where line 9 is:
$result = $db->query_read("Select * from ".TABLE_PREFIX."myTable");

My understanding is that this error means that $db is not an object that PHP recognizes. This led me to try out various things, none of which worked. I tried adding the following to the start of myPHPfile.php:
1. require_once("./global.php"); <--- this starts an infinite loop, and eventually crashes the browser
2. global $vbulletin
3. global $db

I'd like to point out that, thanks to articles posted here, I have been able to use $vbulletin and $db in a regular vb powered custom page without any issues.

Any help appreciated.

Lynne
01-01-2010, 02:18 PM
$db->query_read is defined by vbulletin. If you don't include global.php (or init.php), then it isn't defined. You would have to use standard mysql if you aren't going to include the vbulletin files that allows you to use query_read.

saajjj
01-01-2010, 02:35 PM
Thanks a lot Lynne, init.php did the trick for me.

The posts I read mostly said to add global.php but in my case that introduced a recursive call to the header template. (which eventually crashed the browser).

Incase anyone is reading, I needed to add the following right at the top of myPHPfile.php to get things working:
define('VB_AREA', 'Forum');
define('THIS_SCRIPT', 'myPHPfile');

require_once("./includes/init.php");