MyBB Community Forums

Full Version: Getting username
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
How would I get the username on a page not belonging to the forum? Without using the global.

Thanx
The easiest way that I can think of is if you include the global.php file.
chdir("forum"); // path to MyBB
require "./global.php";
echo $mybb->user['username'];
Or you can create your own mysql query and then display the username that way. Wink
Thanx for your help.

By using your code I get

Fatal error: Call to a member function on a non-object in /var/www/forum/inc/class_datacache.php on line 33

Any idea???
heloo there

please make a new php file

and put this code in

<?php
// hostname or ip of server
$servername='localhost';

// username and password to log onto db server
$dbusername='prefix_user';
$dbpassword='pass';

// name of database
$dbname='prefix_database';

////////////// Do not  edit below/////////

connecttodb($servername,$dbname,$dbusername,$dbpassword);
function connecttodb($servername,$dbname,$dbuser,$dbpassword)
{
global $link;
$link=mysql_connect ("$servername","$dbuser","$dbpassword");
if(!$link){die("Could not connect to MySQL");}
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
}
///////////////////////////Now to show the usernames 
///////u may edit here , the way u want it, it's just an example, it's just the above code that;s used to connect
$rt = $db->query("SELECT uid, username FROM `easyflas_users`);
while($nt = $db->fetch_array($rt)){
$user = "<a href=\"member.php?action=profile&uid=$nt[uid]\">$usr</a>";
}

$user is a variable so put it in a template or in the page.

however u may use the ECHO in case ur just running the php file without templates

echo "$nt[username]";


regards
Hey, I tried it:

On a empty page I get this error

Parse error: parse error, unexpected T_STRING in /var/www/forum/test.php on line 26

Line 26 is:
$user = "<a href="member.php?action=profile&uid=$nt[uid]\">$usr</a>";

If I comment
$rt = $db->query("SELECT uid, username FROM `easyflas_users`);
while($nt = $db->fetch_array($rt)){
$user = "<a href="member.php?action=profile&uid=$nt[uid]\">$usr</a>"; 
out and use

echo "$nt[username]";

The page is complete empty

Using the same in my script I get

Fatal error: Call to undefined function: connecttodb() in /var/www/forum/comment_functions.php on line 46

line 46 is
connecttodb($servername,$dbname,$dbusername,$dbpassword);

I am sure I used the correct settings for the db. Any ideas?
zaher1988 forgot to escape a double quote.
$user = "<a href=\"member.php?action=profile&uid=$nt[uid]\">$usr</a>";
He also forgot to escape a double quote here Smile
$rt = $db->query("SELECT uid, username FROM `easyflas_users`");

But I still get

Fatal error: Call to a member function on a non-object in /var/www/forum/test.php on line 24

in the empty file

and/or

Fatal error: Call to undefined function: connecttodb() in /var/www/forum/comment_functions.php on line 46

in my script Sad
well $db->query should be mysql_query and $db->fetch_array should be mysql_fetch_array
Now I get

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /var/www/forum/test.php on line 25

I put the test.php file as an attachment. I hope this not the fault of the server Sad
Pages: 1 2