ifis Posted November 6, 2007 Share Posted November 6, 2007 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. Quote Link to comment Share on other sites More sharing options...
teng84 Posted November 6, 2007 Share Posted November 6, 2007 put your code under the [code] tags and explain it a bit clearer [/code] Quote Link to comment Share on other sites More sharing options...
trq Posted November 6, 2007 Share Posted November 6, 2007 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. Quote Link to comment Share on other sites More sharing options...
ifis Posted November 6, 2007 Author Share Posted November 6, 2007 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? Quote Link to comment Share on other sites More sharing options...
trq Posted November 6, 2007 Share Posted November 6, 2007 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>"; } ?> Quote Link to comment Share on other sites More sharing options...
ifis Posted November 6, 2007 Author Share Posted November 6, 2007 It worked! Thanks for the help. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.