Login->Display Username

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Login->Display Username

Post by Pazuzu156 »

It's all in the name. I have a login script set up on my website, it's completely basic, but right now, all I want to do is display the username for a like "Hello <username>!"

Here is what I'm doing about it:

snippet from Membership.php

Code: Select all

<?php
function find_username() {
	$username = "SELECT * FROM users WHERE username = ?";
}
?>
Session code:

Code: Select all

<?php
//check if user is logged in
session_start();
require_once('classes/Membership.php');
$membership = new Membership();
$membership->confirm_Member();
?>
Display code on index page:

Code: Select all

<?php require_once('classes/Membership.php');$membership = new Membership();$membership->find_username(); echo "$username"; ?>
I know I have to be doing SOMETHING wrong.
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Re: Login->Display Username

Post by litebearer »

variables are local to functions unless declared global (not a good idea) therefore you should RETURN values created in functions for use outside the function. Capice?
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: Login->Display Username

Post by Pazuzu156 »

Yeah, like, you have to declare the $membership variable each page you have if you are to have anything from my membership page to work. Is that what you're getting at?
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
User avatar
ganesh_dabhade
Forum Newbie
Posts: 19
Joined: Sun Feb 06, 2011 12:42 am
Contact:

Re: Login->Display Username

Post by ganesh_dabhade »

1. If ur script is working fine, u just need to Set the session variable to logged in user and echo the same.
OR
2. ur function should return a value

Code: Select all

<?php
function find_username() {
        $username = "SELECT * FROM users WHERE username = ?";
         return $username;
}
?>
and echo the same.
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: Login->Display Username

Post by Pazuzu156 »

I fixed my SQL query, and even returned $username, here is what it looks like:

Code: Select all

<?php
function find_username() {
    $connect = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die (mysql_error());
    $dbconn = mysql_select_db(DB_NAME) or die (mysql_error());
    $username = mysql_query("SELECT `username` FROM `users` WHERE `username` = `username`") or die (mysql_error());
    return $username;
}
?>
And this is the echo I do to display the username:

Code: Select all

<?php echo "Hello ".$membership->find_username(); ?>
But instead of ending up with my username, I end up with: "Hello Resource id #7"

How do I fix this?
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
Post Reply