Catana Posted May 8, 2014 Share Posted May 8, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/288341-username-from-database-to-display-in-fieldset-legend/ Share on other sites More sharing options...
Ch0cu3r Posted May 8, 2014 Share Posted May 8, 2014 echo the users username inside the <legend> tag? Quote Link to comment https://forums.phpfreaks.com/topic/288341-username-from-database-to-display-in-fieldset-legend/#findComment-1478759 Share on other sites More sharing options...
Azerex Posted May 8, 2014 Share Posted May 8, 2014 (edited) 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 May 8, 2014 by Azerex Quote Link to comment https://forums.phpfreaks.com/topic/288341-username-from-database-to-display-in-fieldset-legend/#findComment-1478794 Share on other sites More sharing options...
Jacques1 Posted May 8, 2014 Share Posted May 8, 2014 That Azerex guy is a troll. He's posting the same garbage script into every thread, regardless of the question. Quote Link to comment https://forums.phpfreaks.com/topic/288341-username-from-database-to-display-in-fieldset-legend/#findComment-1478796 Share on other sites More sharing options...
Azerex Posted May 8, 2014 Share Posted May 8, 2014 (edited) 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 May 8, 2014 by Azerex Quote Link to comment https://forums.phpfreaks.com/topic/288341-username-from-database-to-display-in-fieldset-legend/#findComment-1478797 Share on other sites More sharing options...
Jacques1 Posted May 8, 2014 Share Posted May 8, 2014 Neither your code nor your comments have anything to do with the question. Did you even read the question? I have no idea why you think it's helpful to recycle random code snippets from the 90s. At best, this is an example of how not to do it. Quote Link to comment https://forums.phpfreaks.com/topic/288341-username-from-database-to-display-in-fieldset-legend/#findComment-1478799 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.