Jump to content

[SOLVED] Connecting to MySQL using sessions info


ifis

Recommended Posts

I have a password protected webpage using sessions.  Once the password has been checked, I want to used the stored sessions username to get user specific information from my database.  I cannot get the WHERE section of SELECT*FROM ...WHERE to work.  Here is the login code:

// 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");

// username and password sent from signup form

$myusername=$_POST['myusername'];

$mypassword=$_POST['mypassword'];

 

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

$result=mysql_query($sql);

 

// Mysql_num_row is counting table row

$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row

 

if($count==1){

// Register $myusername, $mypassword and redirect to file "login_sucesswname.php"

session_register("myusername");

session_register("mypassword");

header("location:login_sucesswname.php");

}

else {

echo "Wrong Username or Password";

}

?>

Here is the code trying to connect to db:

<?PHP

session_start();

if (!session_is_registered("myusername"))

{

header("Location: member_login.html");

exit();

}

 

include ("member.inc");

// 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");

 

$result=mysql_query( "SELECT * FROM Member WHERE loginName= 'myusername'");

 

//get the first entry from the result

$row = mysql_fetch_array($result);

 

// print contents

echo "<html><h2>my username is $myusername</h2></html>";

?>

The code connects to the database, but only displays the first rows infor and not the info from a specific username.  thanks.

Link to comment
Share on other sites

Assuming its this query your talking about....

 

$result=mysql_query( "SELECT * FROM Member WHERE loginName= 'myusername'");

 

It would need to be....

 

$result=mysql_query( "SELECT * FROM Member WHERE loginName= '{$_SESSION['myusername']}'");

 

ps: session_register() has long been depricated and is simply no longer needed.

Link to comment
Share on other sites

Thanks for the help.  I changed the session_register to $_SESSION['myusername'] but an still only getting the first row of information from my table instead of the row that is associated with the persons username.  If seems like the WHERE statement is being ignored.

my new code is:

login page (just changed code from above):

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_sucesswname.php"
$_SESSION['myusername']; 

 

Display users name:

 

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql = "SELECT * FROM Member WHERE loginName= '{$_SESSION['myusername']}'";
$result=mysql_query($sql);

//get the first entry from the result
$row = mysql_fetch_array($result);

// print contents
echo "<html><h2>my username is $myusername</h2></html>";
echo "my name is $row[firstName] $row[lastName]";
?>

any solutions?

Link to comment
Share on other sites

Here, I'll simply give you an example of what your code should look like.

 

<?php

  // connect to db.

  if (isset($_POST['submit'])) {
    $myusername = $_POST['myusername']; 
    $mypassword = $_POST['mypassword']; 
    $sql = "SELECT * FROM $tbl_name WHERE loginName = '$myusername' and `password` = '$mypassword'";
    if ($result = mysql_query($sql)) {
      if (mysql_num_rows($result)) {
        session_start();
        $_SESSION['myusername'] = $myusername;
        header("location:login_sucesswname.php");
      } else {
        echo "Wrong Username or Password";
      }
    }

?>

<?php

  session_start();
  if (!isset($_SESSION['myusername'])){
    header("Location: member_login.html");
    exit();
  } else {
    echo "<html><h2>my username is {$_SESSION['myusername']}</h2></html>";
  }

?>

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.