Jump to content

Recommended Posts

Hi,

I was recommended this site by someone I know, so I hope you'll be able to help :)

 

I am currently building a website template site, but am having some difficulty with a members set-up.

 

What I would like it to do..

I would like to be able to retrieve values from a database table using the username of a member.

 

E.g.

When someone logs into their account, it will display in the place I would like it..[in the 'Account Information Section', click the link at the base of the post to see where I mean] 'Welcome, USERNAME'

I would also be able to get other details retaining to that username, such as its ID [Defined in the MySQL table].

 

Thanks,

Luke

 

The Website: http://www.templateking.co.uk

Link to comment
https://forums.phpfreaks.com/topic/84779-solved-help-needed-to-retrieve-values/
Share on other sites

I have tried all sorts of things, but have come to no avail.

 

I would like to know how to get the username of the member logged in, and how I can use that to retrieve other values from the table the information is located in.

 

---

 

I dont want you to write anything for me. Just looking for some guidance really.

 

I am using this as my login script.

<?php

ob_start();

$host="--REMOVED--"; // Host name

$username="--REMOVED--"; // Mysql username

$password="--REMOVED--"; // Mysql password

$db_name="--REMOVED--"; // Database name

$tbl_name="members"; // Table name

 

// Connect to server and select databse.

mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");

 

$myusername=$_POST['myusername'];

$mypassword=$_POST['mypassword'];

 

 

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";

$result=mysql_query($sql);

if($result){

echo "go";

}

 

//mysql_num_row is counting table row

$count=mysql_num_rows($result);

//if result match  $myusername  and mypassword  table row must be 1 row

 

if($count==1){

//session_register("myusername");

session_start();

$_SESSION['myusername'] = $myusername;

header("location:index.php");

}

else {

echo "Wrong Username or Password";

}

ob_end_flush();

?>

 

If I try to echo $_SESSION['username'] it just appears as a blank space.

every page needs session_start(); at the beging to set and see the session's

 

<?php
ob_start();
session_start();
$host="--REMOVED--"; // Host name
$username="--REMOVED--"; // Mysql username
$password="--REMOVED--"; // Mysql password
$db_name="--REMOVED--"; // Database name
$tbl_name="members"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];


$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
if($result){
echo "go";
}

//mysql_num_row is counting table row
$count=mysql_num_rows($result);
//if result match  $myusername  and mypassword  table row must be 1 row

if($count==1){
//session_register("myusername");
$_SESSION['myusername'] = $myusername;
header("location:index.php");
}
else {
echo "Wrong Username or Password";
}
ob_end_flush();
?>

   <form method="POST" action="login.php">
  <input name="myusername" type="text" size="13" />
  <input name="mypassword" type="password" size="13" />
  <input type="submit" name="Submit" value="Login!" />
  </form>

 

This is the form which calls the script. Located on the index page.

 

-----

I meant to type that ;). Everytime I try to echo the $_SESSION['myusername'] it simply returns a blank space.

I've updated the script, so that the session_start() is at the top now.

 

I all added this piece of code to the header file, [it will appear in the section 'Account Information']

 

  <?php 
                          if(isset($_SESSION['myusername'])) {
          echo "Welcome,  ". $_SESSION['myusername'];
          } else {
          echo "Welcome, Guest";
          }
                          ?>

 

Even when I login, it still display's 'Welcome, Guest'.. which indicates something is not working.

The login.php script

<?php
session_start();
ob_start();

$host="REMOVED"; // Host name
$username="REMOVED"; // Mysql username
$password="REMOVED"; // Mysql password
$db_name="REMOVED"; // Database name
$tbl_name="members"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$myusername=$_POST['myusername'];
$mypassword=md5($_POST['mypassword']);

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
if($result){
echo "go";
}

//mysql_num_row is counting table row
$count=mysql_num_rows($result);
//if result match  $myusername  and mypassword  table row must be 1 row

if($count==1){
//session_register("myusername");
$_SESSION['myusername'] = $myusername;
header("location:index.php");
}
else {
echo "Wrong Username or Password";
}
ob_end_flush();
?>

 

The code in the header file. [Appears in the Application Information Section]

    <?php 
                          if(isset($_SESSION['myusername'])) {
          echo "Welcome,  ". $_SESSION['myusername'];
          } else {
          echo "Welcome, Guest";
          }
         
                          ?>

I hadn't put 'session_start()' in the header file.

 

Welcome, Luke

 

Its working now :D.

 

The last thing, was how do I use that name to get more values from the 'members' table for that username.

 

I have tried to do things like ..

 

<?php
$user = $_SESSION['username'];
$info="SELECT * FROM members WHERE username='$user'";
$result_info = mysql_query($info);
?>

 

Which, when using this line, displays nothing.

echo "ID: ". $result_info['ID'];

You're missing a line. You need to grap the results first, using one of the mysql_fetch_xxxx functions:

 

<?php
$user = $_SESSION['username'];
$info="SELECT * FROM members WHERE username='$user'";
$result_info = mysql_query($info) or die(mysql_error());
$row = mysql_fetch_assoc($result_info);
echo "ID: ". $row['ID'];//this assumes you have a field called ID
?>

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.