Page 1 of 1

Login->Display Username

Posted: Sat Feb 19, 2011 10:13 pm
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.

Re: Login->Display Username

Posted: Sat Feb 19, 2011 11:37 pm
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?

Re: Login->Display Username

Posted: Sun Feb 20, 2011 12:11 am
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?

Re: Login->Display Username

Posted: Sun Feb 20, 2011 7:05 am
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.

Re: Login->Display Username

Posted: Mon Feb 21, 2011 8:18 am
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?