Retrieve data from database using session variable - php

<?php
{
session_start();
include "dbconnect.php";
$email = $_SESSION['email'];
echo $email;
$query = "SELECT uid FROM master WHERE emailid = '$email' ";
$result = mysql_query($query);
$row=mysql_fetch_array($result);
while($row)
{
$uid=$row[1];
echo $uid;
}
}
whats wrong in this code.it is giving the email but not able to retrieve uid using session variable email.
please help

while($row) {
$uid = $row[1];
echo $uid;
}
should be
while($row) {
$uid=$row['uid'];
echo $uid;
}
Try if this code works for you
session_start();
include "dbconnect.php";
$email = $_SESSION['email'];
//echo $email;
$query = "SELECT uid FROM master WHERE emailid = '$email' ";
$result = mysql_query($query);
while($row=mysql_fetch_array($result)){
$uid=$row['uid'];
echo $uid;
}

I don't know why you have { at beginning of your code, you should delete that. And second thing, delete echo $email; and you should have working code

try $uid=$row[0] instead of $uid=$row[1];
Because $row is an array.Array always starts its counter form 0.
here you have defined $uid=$row[1] which means it will display the result from second row of the result set..But there is no second row there is only one row which is "uid" so try $uid=$row[0] instead of $uid=$row[1];

try this:
<?php
session_start();
include "dbconnect.php";
$email = $_SESSION['email'];
$query = "SELECT uid FROM master WHERE emailid = '".$email."'";
$result = mysql_query($query);
if(mysql_num_rows($result)>0)
{
$row = mysql_fetch_array($result);
$uid = $row["uid"];
echo $uid;
}
else
{
echo "No record found";
}
?>

Related

how can i call a function that return user array from their id from another file?

This functions is used to get id of the created user
$logged_in_user_id = mysqli_insert_id($db);
$_SESSION['user'] = getUserById($logged_in_user_id); // put logged in user in session
This is the function declared that return user array from their id:
function getUserById($id){
$query = "SELECT * FROM users WHERE id=" . $id;
$result = mysqli_query($db, $query);
$user = mysqli_fetch_assoc($result);
return $user;
}
//I'm trying to call it in this file:
//update.php
<?php
include('functions.php');
$id=$_GET['id'];
$id= getUserById($id);
$view = "SELECT * from users where id = '$id'";
$result = mysqli_query($db,$view);
$row = $result->fetch_assoc();
if (isset($_POST['update'])) {
$id= $_SESSION['id'];
$username= $_POST['username'];
$email= $_POST['email'];
$insert= "UPDATE users set username= '$username',
email = '$email' where id='$id'";
if ($db->query($insert) ==TRUE) {
echo "Sucessfully update data";
header('location:update.php');
}else{
echo "Ooppss cannot add data" . $db->error;
header('location:update.php');
}
$db->close();
}
?>
the first code is where you can see :This functions is used to get id of the created user
the second one is where you see the the function declared that return user array from their id
please can someone help me with this?

Session value are not set in PHP

index.php
<?php
if(isset($_POST['submit']))
{
$email = $_POST['email'];
$password = $_POST['password'];
$sql = "select * from admin where email = '$email' and password = '$password'";
$result = mysqli_query($con,$sql);
$num_rows = mysqli_num_rows($result);
if($result)
{
if ($num_rows > 0)
{
$sqls = "select * from admin where email = '$email' and password = '$password'";
$results = mysqli_query($con,$sqls);
while ($rows = mysqli_fetch_assoc($results))
{
$_SESSION['admin_id'] = $rows['id'];
}
header ("Location: dashboard.php");
}
else
{
echo "<p id='red'>Wrong email or password.</p>";
}
}
}
?>
dashboard.php
<?php
session_start();
include('../config.php');
$admin_id = $_SESSION['admin_id'];
echo $admin_id;
?>
In this code I have create login form and initialize id into session variable when I echo $_SESSION['admin_id'] in index.php it return value but when I want to set $_SESSION['admin_id'] in dashboard.php but its not working or showing no value. How can I fix this issue ?Please help me.
Thank You
Add session_start(); to the top of index.php (inside the php tags of course)
session_start();
Is the session_start() already initialized in the index.php file? You should check that first. It's recommended that you place this in top of all files or a single included file in your script.
session_start();
Test if this fixes your problem.
Before set or get session in php you should always start session at the top of the page like below
session_start();
So in your code try to to start session in index.php
Add session_start() in index.php
<?php
session_start();
if(isset($_POST['submit']))
{
$email = $_POST['email'];
$password = $_POST['password'];
$sql = "select * from admin where email = '$email' and password = '$password'";
$result = mysqli_query($con,$sql);
$num_rows = mysqli_num_rows($result);
if($result)
{
if ($num_rows > 0)
{
$sqls = "select * from admin where email = '$email' and password = '$password'";
$results = mysqli_query($con,$sqls);
while ($rows = mysqli_fetch_assoc($results))
{
$_SESSION['admin_id'] = $rows['id'];
}
header ("Location: dashboard.php");
}
else
{
echo "<p id='red'>Wrong email or password.</p>";
}
}
}
?>

Session Variables are lost after redirecting using header()

I have got index.php file that takes username and password from users, then it redirects to process_login.php that compares these credentials with SQL database to authorize the users. Now if the user is authorized, I want to get all the data about this user and want to use in other PHP files. I am using sessions to do so, but somehow they are not working.
I know they are so many similar questions, but none of them worked.
Here is my process_login.php code
<?php
session_start();
require_once('connectdatabase.php');
if(isset($_POST) && !empty($_POST)) {
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM users WHERE USERNAME='$username' AND PASSWORD='$password'";
$result = mysqli_query($connection, $sql);
echo $count = mysqli_num_rows($result);
if($count == 1) {
$row = mysqli_fetch_assoc($result);
$_SESSION['first_name'] = $row["FIRST_NAME"];
$_SESSION['last_name'] = $row["LAST_NAME"];
$_SESSION['email'] = $row["EMAIL"];
$_SESSION['username']=$username;
header('Location: ../../src/welcome.php');
exit();
}
else {
header('Location: ../../src/index.php');
}
}
?>
Now I want those variables on welcome.php file.
And this is my welcome.php code
<?php
session_start();
$fist_name = $_SESSION['first_name'];
echo "<script>console.log('$first_name');</script>";
?>
It's because you are using $fist_name rather than $first_name. And edit your echo part
<?php
session_start();
$fist_name = $_SESSION['first_name'];
echo "<script>console.log('$first_name');</script>";
?>
To
<?php
session_start();
$first_name = $_SESSION['first_name'];
echo $first_name;
?>
I wanted to comment but I can't so here is my suggestion for you.
When something like your issue happens to me I tend to echo the $_SESSION all of them to see if they're actually set or not.
Below is a small PHP script which does the same but I'm using PDO as the DB API.
if (isset($_REQUEST["pWord"])){
$inmPword = md5($_REQUEST["pWord"]);
$loginData = "SELECT * FROM userlogin WHERE pWord = :pWord";
$loginDataQuery = $dbConnect -> prepare($loginData);
$loginDataQuery -> bindParam(':pWord', $inmPword);
$loginDataQuery -> execute();
if ($row = $loginDataQuery -> fetch(PDO::FETCH_ASSOC)){
//Time to set the session
$_SESSION["uId"] = $row["uId"];
$_SESSION["uRole"] = $row["uRole"];
$_SESSION["fName"] = $row["fName"];
$_SESSION["lName"] = $row["lName"];
echo "3";
}else{
echo "4";
}
}
I think it's better not do the row count and echo it. Something like this might help.
$sql = "SELECT * FROM users WHERE USERNAME='$username' AND PASSWORD='$password'";
$result = mysqli_query($connection, $sql);
if($row = mysqli_fetch_assoc($result)) {
$_SESSION['first_name'] = $row["FIRST_NAME"];
$_SESSION['last_name'] = $row["LAST_NAME"];
$_SESSION['email'] = $row["EMAIL"];
$_SESSION['username']=$username;
header('Location: ../../src/welcome.php');
exit();
}

get ID for a specific user in mysql database in PHP

When a user signs in i want to echo back there ID (which is created because of the auto_increment in phpMyAdmin) from there account, here's my login.PHP:
<?php
$conn = mysqli_connect("xxxxxx", "xxxxx", "xxxx", "BuyerAccounts");
$Email = $_POST["Email"];
$Password = $_POST["Password"];
$sql_query = "select Buyer_Email from user_info where Buyer_Email like '$Email' and Buyer_Password like '$Password';";
$result = mysqli_query($conn, $sql_query);
if(mysqli_num_rows($result) > 0 ){
$row = mysqli_fetch_assoc($result);
$name = $row["Buyer_Email"];
echo "Welcome: Buyer";
}else{
$int = 1;
//echo "Buyer login failed...";
}
}else{
echo "Login failed...";
}
}
mysqli_stmt_close($statement);
mysqli_close($conn);
?>
Add the column name id in your sql query.let say your column name for id is ID
$sql_query = "select ID,Buyer_Email from user_info where Buyer_Email like '$Email' and Buyer_Password like '$Password';";
$result = mysqli_query($conn, $sql_query);
if(mysqli_num_rows($result) > 0 ){
$row = mysqli_fetch_assoc($result);
$name = $row["Buyer_Email"];
$user_id = $row['ID'];
echo $user_id;
echo "Welcome: Buyer";
}
Since your making login in php its good choice to use $_SESSION.
All you need to do is add a session_start(); at the top of any php script where you need to use session.
<?php
session_start();
$conn = mysqli_connect("xxxxxx", "xxxxx", "xxxx", "BuyerAccounts");
$Email = $_POST["Email"];
$Password = $_POST["Password"];
$sql_query = "select ID,Buyer_Email from user_info where Buyer_Email like '$Email' and Buyer_Password like '$Password';";
$result = mysqli_query($conn, $sql_query);
if(mysqli_num_rows($result) > 0 ){
$row = mysqli_fetch_assoc($result);
$name = $row["Buyer_Email"];
$user_id = $row['ID'];
//using session
$_SESSION["user_id"] = $user_id;
echo $user_id;
echo "Welcome: Buyer";
}
Now you can access anywhere in your php script using the $_SESSION variable.
echo $_SESSION["user_id"] ;
Let's start from the beginning. You create a login form, you store sessions based on the values:
login.php
session_start();
$_SESSION["username"] = $username;
main.page
$username = $_SESSION["username"];
echo "Hi $username";
EDIT 2
Ok, so you want to check if username exists and then echo their ID. Regardless, almost all login systems have sessions.
After logging in, let's say you have a $_SESSION of id.
php
session_start();
$id = $_SESSION["id"];
$db = mysqli_connect("xxxxxx", "xxxxx", "xxxx", "BuyerAccounts");
$check = $db->query("SELECT * FROM users WHERE id='$id'");
$num_check = $check->num_rows;
$fetch_check = $check->fetch_object();
$id2 = $fetch_check->id;
if($num_check) {
// User Exists
echo $id2;
} else {
echo "You don't exist."
}
Please note, normally, I would just echo $id. However, the OP requested to echo the id from the db, so I echoed $id2.

Trying to store session variable from result of SQL query

I have tried everything from storing the query as a variable and using the fetch_array but it keeps just giving me the output "Array" when I try to print $_SESSION['permission']. I have no idea what to do please help.
<?php
session_start();
mysql_connect("localhost","","");
mysql_select_db("a");
if(isset($_SESSION['loggedin']))
{
header("Location: http://www.mywebsite.com/whatever");
}
if($_POST['submit'])
{
$name = mysql_real_escape_string($_POST['username']);
$pass = mysql_real_escape_string($_POST['password']);
$mysql = mysql_query("SELECT * FROM users WHERE name = '{$name}' AND password = '{$pass}'");
$result = mysql_query("SELECT permission FROM users WHERE name = '{$name}' AND password = '{$pass}'");
$permission = mysql_fetch_array($result);
if(mysql_num_rows($mysql) < 1)
{
die("Password was incorrect!");
}
$_SESSION['loggedin'] = "YES";
$_SESSION['name'] = $name;
$_SESSION['permission'] = $permission;
header ("Location: http://www.mywebsite.com/whatever");
}
echo "<form type='login.php' method='POST'>
Username: <br>
<input type='text' name='username'><br>
Password: <br>
<input type='password' name='password'><br>
<input type='submit' name='submit' value='Login'>
</form>";
?>
You need to traverse the resultset using a loop,
while($row = mysql_fetch_array($result)) {
//use each row value($row) as per your need
}
So, I believe, you want to achieve something like this,
if(isset($_POST['submit']))
{
$name = mysql_real_escape_string($_POST['username']);
$pass = mysql_real_escape_string($_POST['password']);
$result = mysql_query("SELECT permission FROM users WHERE name = '{$name}' AND password = {$pass}'");
$permission = mysql_fetch_array($result);
$mysql = mysql_query("SELECT * FROM users WHERE name = '{$name}' AND password = '{$pass}'");
if(mysql_num_rows($mysql) < 1)
{
die("Password was incorrect!");
}
$_SESSION['loggedin'] = "YES";
$_SESSION['name'] = $name;
$i = 0;
while($row = mysql_fetch_array($result)) {
$_SESSION['permission' . $i] = $row;
$i++;
}
header ("Location: ");
}
If $row is a result of a query you need to specify a column like $_SESSION['permission'] = $row[column_name];
Just edited the POST part of your code. Remember that you must fetch the result and use the associative array to take the value of an specific column:
if($_POST['submit'])
{
$name = mysql_real_escape_string($_POST['username']);
$pass = mysql_real_escape_string($_POST['password']);
$result = mysql_query("SELECT * FROM users WHERE name = '{$name}' AND password = {$pass}'");
if(!$row = mysql_fetch_array($result))
{
die("Password was incorrect!");
}
$_SESSION['loggedin'] = "YES";
$_SESSION['name'] = $name;
$_SESSION['permission'] = $row['permission'];
header ("Location: ");
}
There is no need to run the query twice.
You are assigning the row array to the $_SESSION['permission'] variable so when you print it you get array.
Try using print_r on that array and you'll see what's in it. Example: print_r($_SESSION['permission']); Print cannot print an array. Use print_r or var_dump to find out what's going on when you see this happen again.
http://php.net/manual/en/function.var-dump.php
http://php.net/manual/en/function.print-r.php

Categories