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.

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.

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?

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>";
  }

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.