Jump to content

Username from database to display in fieldset legend?


Catana

Recommended Posts

Hi,

 

Does anyone know how to grab a database name from a user table to display that name within a fieldset legend?

 

I have a forum with a number of posts (and have wrapped a fieldset around each post and want the legend to display the name of the user who posted it), and just need a bit of background knowledge on what sort of query I would be looking at to achieve this, thanks. 

Link to comment
Share on other sites

so the php could be something like this.

// Database Variables
define('DB_HOST', 'Database Host (Normally "localhost" if your script is on the same ip as the db');
define('DB_USER', 'Database User');
define('DB_PASSWORD', 'Database Password');
define('DB_DATABASE', 'Database Name');;

//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
    die('Failed to connect to server: ' . mysql_error());
}

//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
    die("Unable to select database");
}
 
$table = "table you are pulling data from";
$username = $_SESSION['username'];

$query = "SELECT * FROM `table_name` WHERE username = ".$username;
//This is just an example where you can replace username with any column
 
$result = mysql_query($query) or die(mysql_error());
 
 
//Grab Information from query
 
if (mysql_numrows ($result) >= 1) {
        $rows = mysql_fetch_array ($result, MYSQL_ASSOC);

        foreach($rows AS $k => $v) {
            $$k = $v;
        }
       
}

you can put the above code at the top of the file.

 

then you can just go to where your legend tag is and eg...

<fieldset>
<legend>
<?php echo $username; ?>
</legend>
</fieldset>
Edited by Azerex
Link to comment
Share on other sites

No I'm not. I posted the above code in 1 other thread because it was relevant. I have just decided to edit my current code because since he isn't making a website of his own he is using forum software the current user should already be defined. If you know how to read you can see that there are differences in the code from the other post. Learn to read before calling someone a troll smart one.

 

[uPDATE]

 

It won't let me edit the first post so ignore the first post and follow this:

 

if your forum already keeps track of the currently logged in player you should find where that is defined and then echo that value on the page.

 

ex:

<?php
$user = $_SESSION['username']; // This is an example of the definition
 
$echo $user; // This will echo the username of the current person logged in.

so if you find the definition you just go to the page you want to add it to and then inject php into that page by doing this eg...

 

 

<fieldset>
<legend>
<?php echo $user; ?>
</legend>
</fieldset>
Edited by Azerex
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.