0

I'm using a mySQL table called users that includes fields such as "userID, username, and password".
When users login, the form passes the "username" and "password" entries into an authentication.php file which then does a
SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword.

I want to run a PHP query to create a new variable equal to "userID" in the table where "username" equals a variable called $username. Then creates the new variable such as
$_SESSION["userID"] = $userID

How can I create another variable from the userID data that was pulled.

The script lines below are for when its checking the username and password and then creating the session variables from them:

$sql="SELECT * FROM $tbl_name WHERE username='$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){
echo "Welcome back ";
echo $myusername;
echo "!";

// Create session vars $myusername, $mypassword and redirect to file "success.php"
$_SESSION["myusername"] = $myusername;
$_SESSION["mypassword"] = $mypassword; 
}
else {
echo "Wrong Username or Password";
}

1 Answer 1

1

For this you need to iterate through your result in a while loop. mysql_fetch_array will do this. The following code will create a session object.

$result = mysql_query($sql);
    while ($row = mysql_fetch_array($result)) {
        echo $row['userid'];
    }
Sign up to request clarification or add additional context in comments.

2 Comments

When I use that I get this error mysql_fetch_array() expects parameter 1 to be resource, string given.
Sorry I didn't notice $sql was a string. You need to convert that to a db object. mysql_query will do that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.