Conflict between PDO and phpbb DBAL

Discussion forum for MOD Writers regarding MOD Development.
Locked
User avatar
Alien_Time
Registered User
Posts: 389
Joined: Fri Dec 31, 2010 7:08 am

Conflict between PDO and phpbb DBAL

Post by Alien_Time » Sun Feb 09, 2014 8:32 pm

I believe phpbb is using its own DBAL, is that right? I am basically creating an application that runs on phpbb framework. However, in this app I have created a separate mysql table and using PDO for all database related queries. However, when I use PDO, I am getting an error and I am assuming it could be due to the method used in phpbb's DBAL. My phpbb custom page for this application looks something like this:

Code: Select all

<?php
define('IN_PHPBB', true);
$this_dir = './';
$phpbb_root_path = $this_dir . '../';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/functions_display.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();

//THIS IS THE START OF THE APP CODES
include('includes/db_PDO_connect.php');

...
...
...

page_header('Title');

$template->set_filenames(array(
    'body' => 'app/body.html',
));

page_footer();
The db_PDO_connect.php file is like this:

Code: Select all

	$db = new PDO('mysql:dbname=app_trial;host=127.0.0.1;charset=utf8', 'root', '');	
	$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
	$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
When I include the above PDO connect into phpbb's custom page, I am receiving the following error:

Code: Select all

Fatal error: Call to undefined method PDO::sql_query() in C:\Users\user\Documents\EasyPHP-5.3.6.0\www\forum\includes\functions.php on line 4181
How can I solve this error? How can I use PDO queries in custom pages without conflicting with phpbb's DB abstract?

User avatar
Alien_Time
Registered User
Posts: 389
Joined: Fri Dec 31, 2010 7:08 am

Re: Conflict between PDO and phpbb DBAL

Post by Alien_Time » Sun Feb 09, 2014 9:10 pm

I was thinking, should wrapping the PDO into a separate class do the trick? Is that the right way to do it?

User avatar
T0ny
Registered User
Posts: 1383
Joined: Sun Jan 29, 2006 8:42 pm
Location: Lancashire
Name: Tony

Re: Conflict between PDO and phpbb DBAL

Post by T0ny » Sun Feb 09, 2014 10:14 pm

phpbb uses the variable name $db for its database object. Your code is overwriting that variable, which is the cause of the error message. You need to choose a different variable name e.g. $pdo_db

User avatar
Alien_Time
Registered User
Posts: 389
Joined: Fri Dec 31, 2010 7:08 am

Re: Conflict between PDO and phpbb DBAL

Post by Alien_Time » Mon Feb 10, 2014 11:34 am

T0ny wrote:phpbb uses the variable name $db for its database object. Your code is overwriting that variable, which is the cause of the error message. You need to choose a different variable name e.g. $pdo_db
Good point! Makes sense! Thank you for spotting this and telling me this. Last night I tried wrapping the pdo into a class and that worked but I would need to re-write all the other codes if I was gonna change them to a class. Would prefer to keep it the original method since it is also easier to maintain. If changing the variable works then its excellent. Thanks again.. I will give it a try now.

Cheers!

Locked

Return to “[3.0.x] MOD Writers Discussion”