Access user table data

Discussion forum for Extension Writers regarding Extension Development.
User avatar
HarbourPilot
Registered User
Posts: 25
Joined: Wed Feb 04, 2015 6:13 pm

Access user table data

Post by HarbourPilot » Wed Dec 30, 2015 6:59 pm

Hello,

I'm adding two new columns to the PHPBB user table for a project I'm developing in a custom page.
I'm stook at the sql code. How to access these new columns?

$sql = 'SELECT column_name FROM xxx_users WHERE username='???';

How to fill the username?
The overall_header file does the appropriate connection to the database, or I'll have to do it myself through another file?

Many thanks.

LaxSlash1993
Registered User
Posts: 182
Joined: Sat Sep 22, 2012 2:20 am

Re: Access user table data

Post by LaxSlash1993 » Fri Jan 01, 2016 2:04 am

Try:

Code: Select all

$this->user->data['username']
You will, of course, need the 'user' dependency.

User avatar
HarbourPilot
Registered User
Posts: 25
Joined: Wed Feb 04, 2015 6:13 pm

Re: Access user table data

Post by HarbourPilot » Fri Jan 01, 2016 2:55 am

Do you mean like this?

$sql = 'SELECT column_name
FROM ' . USERS_TABLE . '
WHERE $this->user->data['username'];

LaxSlash1993
Registered User
Posts: 182
Joined: Sat Sep 22, 2012 2:20 am

Re: Access user table data

Post by LaxSlash1993 » Fri Jan 01, 2016 3:24 am

Yup, that seems like it should work. Note, I haven't tested that out, but that's definitely the variable that you're looking for.

User avatar
Brf
Support Team Member
Support Team Member
Posts: 51631
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}
Contact:

Re: Access user table data

Post by Brf » Fri Jan 01, 2016 3:59 am

Why not just skip the SQL statement and use $this->user->data['your_new_column'] ?

LaxSlash1993
Registered User
Posts: 182
Joined: Sat Sep 22, 2012 2:20 am

Re: Access user table data

Post by LaxSlash1993 » Fri Jan 01, 2016 4:27 am

Brf wrote:Why not just skip the SQL statement and use $this->user->data['your_new_column'] ?
So wait.... any column in that table will automatically go to user->data now? Interesting.... or has that always been like that (since 3.0)? Been a couple years since I wrote a phpBB Mod lol

User avatar
Paul
Infrastructure Team Leader
Infrastructure Team Leader
Posts: 24993
Joined: Sat Dec 04, 2004 3:44 pm
Location: The netherlands.
Name: Paul Sohier
Contact:

Re: Access user table data

Post by Paul » Fri Jan 01, 2016 10:09 am

That has been the case for phpBB3 and I think even phpBB2 (But not fully sure about that anymore)
Knock knock
Race condition
Who's there?

My BlogMy Photosmy phpBB Extensionscustom phpBB work & Development

User avatar
HarbourPilot
Registered User
Posts: 25
Joined: Wed Feb 04, 2015 6:13 pm

Re: Access user table data

Post by HarbourPilot » Fri Jan 01, 2016 10:52 am

Code: Select all

$username=$user->data['username'];
$sql = "SELECT user_id 
        FROM table_users 
        WHERE username='$username' ";
$result = $db->sql_query($sql);
$row = $db->sql_fetchrow($result);
$db->sql_freeresult($result);
echo $row['user_id'];
Gentleman,

The above code is working.
The problem is that it only works in the main php file.
In both template file within

Code: Select all

<!-- PHP -->code<!-- ENDPHP -->
and within a included external file

Code: Select all

<!-- INCLUDEPHP file.php -->
it is not working.
It runs without giving errors but does not show anything on the screen.

Any suggestions?

The

Code: Select all

$this->user->data['username']
was givin internal error message.

Bruninoit
Registered User
Posts: 499
Joined: Thu Jun 13, 2013 2:34 pm
Location: Florence (Italy)
Name: Bruno Andrew
Contact:

Re: Access user table data

Post by Bruninoit » Fri Jan 01, 2016 11:01 am

Why do you want to use it in template? Use in php file and so with assign_variable pass the variable to template.

User avatar
HarbourPilot
Registered User
Posts: 25
Joined: Wed Feb 04, 2015 6:13 pm

Re: Access user table data

Post by HarbourPilot » Fri Jan 01, 2016 11:24 am

Bruninoit wrote:Why do you want to use it in template? Use in php file and so with assign_variable pass the variable to template.
Thanks for the help.
Using the

Code: Select all

$template->assign_vars(array(
    'S_SOME_VARIABLE'   => $some_variable,
    )
);
I managed to send the value to the template file, but I still need to assign its value to a variable so I can use it within a php code in a included php file.

Code: Select all

$variable={S_SOME_VARIABLE};
does not work, even within quotes.

How to do it?

User avatar
RMcGirr83
Recognised Extension Developer
Posts: 21032
Joined: Wed Jun 22, 2005 4:33 pm
Location: Your display
Name: Rich McGirr
Contact:

Re: Access user table data

Post by RMcGirr83 » Fri Jan 01, 2016 11:38 am

HarbourPilot wrote:I'm adding two new columns to the PHPBB user table for a project I'm developing in a custom page.
If your custom PHP page is including this at the top of it

Code: Select all

define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);

// Start session management
$user->session_begin();
 
then you will have all the information for the user that is visiting that page via the $user object. You don't need to include another file into your custom file to include that information. Of course if your custom file is outside the root directory of the forum you would need to change the root path variable.
In times of change, learners inherit the earth, while the learned find themselves beautifully equipped to deal with a world that no longer exists - Eric Hoffer
Former Modifications/Extensions Team Member | My extensions
Appreciate the extensions/mods/support then buy me a beer
All requests for support via PM will be ignored

User avatar
HarbourPilot
Registered User
Posts: 25
Joined: Wed Feb 04, 2015 6:13 pm

Re: Access user table data

Post by HarbourPilot » Fri Jan 01, 2016 11:55 am

RMcGirr83 wrote:
HarbourPilot wrote:I'm adding two new columns to the PHPBB user table for a project I'm developing in a custom page.
If your custom PHP page is including this at the top of it

Code: Select all

define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);

// Start session management
$user->session_begin();
 

then you will have all the information for the user that is visiting that page via the $user object. You don't need to include another file into your custom file to include that information. Of course if your custom file is outside the root directory of the forum you would need to change the root path variable.
I have the main php file (with this code you gave, same as in this tutorial), the template file and another php included in the template. The included file is running a script for a project which needs this new columns user info.
And I cannot access the $user object within the included php.
How to?

Bruninoit
Registered User
Posts: 499
Joined: Thu Jun 13, 2013 2:34 pm
Location: Florence (Italy)
Name: Bruno Andrew
Contact:

Re: Access user table data

Post by Bruninoit » Fri Jan 01, 2016 11:59 am

You cannot include the second php file in the main?

User avatar
HarbourPilot
Registered User
Posts: 25
Joined: Wed Feb 04, 2015 6:13 pm

Re: Access user table data

Post by HarbourPilot » Fri Jan 01, 2016 12:04 pm

Bruninoit wrote:You cannot include the second php file in the main?

The file is included in the template like this:

Code: Select all

<!-- INCLUDEPHP somefile.php -->
not in the main php.

Bruninoit
Registered User
Posts: 499
Joined: Thu Jun 13, 2013 2:34 pm
Location: Florence (Italy)
Name: Bruno Andrew
Contact:

Re: Access user table data

Post by Bruninoit » Fri Jan 01, 2016 12:06 pm

Eh I understand... But it's wrong for the phpBB normal scheme... Php stay with php, HTML with HTML in template.

Post Reply

Return to “Extension Writers Discussion”

Who is online

Users browsing this forum: No registered users and 4 guests