I've been stuck with this problem. I have a form where the user will enter his/her input. Based on that input, a query is done to the database that will return matching results.I
t's fairly simple but couldn't find a way to do it.
I manage to load the data using loadResult().
But now since I want to load from multiple columns, loadResult() is a no go.
<?php
$db = JFactory::getDbo();
$name = JRequest::getVar('name');
$query="SELECT username FROM jos_users WHERE name='$name'";
$db->setQuery($query);
echo $db->loadResult();
?>
That was my code when I was using loadResult(). No problem.
But now I want to load username and status from the query. How can I do that?
I tried putting in
<?php
$db = JFactory::getDbo();
$name = JRequest::getVar('name');
$query="SELECT username, status FROM jos_users WHERE name='$name'";
$db->setQuery($query);
$db->loadObject($name);
echo "Username : $name->username";
echo "Status : $name->status";
?>
But returns an error.
Try change:
$db->loadObject($name);
echo "Username : $name->username";
echo "Status : $name->status";
to:
$row = $db->loadRowList();
echo "Username : ".$row['username'];
echo "Status :".$row['status'];
Your code is supposed to be like this.
You have to store return value in a variable and then you can use it. But in your code you are trying to print value with the params send to loadObject method.
<?php
$db = JFactory::getDbo();
$name = JRequest::getVar('name');
$query="SELECT username, status FROM jos_users WHERE id='$name'";
$db->setQuery($query);
$row = $db->loadObject();
echo "Username : $row->username";
echo "Status : $row->status";
?>
Related
This question already has answers here:
How can I get an unknown username given an ID?
(2 answers)
Closed 1 year ago.
am new here, i have an issue with displaying logged in user profile, hoping you guys can help, here is my code :
<?php session_start(); include 'dpconfig.php'
<?php $run = mysqli_query($conn,"Select * from user Where id = $_SESSION['uid]");
$row = mysqli_fetch_array($run, MYSQLI_BOTH); { }
$showid = $row[0];
$showfirst = $row[1];
$showlast = $row[2];
$showuid = $row[3];
echo $showid; echo $showfirst; echo $showlast; echo $showuid;
Now basically this code gives me the details of the first id in my database even if i login different users, i need help selecting data from table name(user) to display logged in user profile, using sessions. Thanks
$run = mysqli_query($conn,"Select * from user where username='xxx' and pass='xx'");
it will return login user detail.
Try something like this, you need to remember to check that the array holds values as well, then you can respond to it...
$conn = dbconfig;
$id = $_SESSION['id'];
$sql = "SELECT * FROM user WHERE id='$id'";
$check = mysqli_query($conn, $sql) or die ("err $id " . mysqli_error ($conn));
$check2 = mysqli_num_rows($check);
if ($check2 != 0) {
while ($row = mysqli_fetch_assoc($check)) {
$userid = $row['id']; // repeat for all db columns you want
}
}
Sorry if there are any typos, done this on my phone quickly. If you need further help gimme a shout.
Updated the code to show more information with the error message, to help you get to the bottom of why it's not working for you.
I am new to Android Programming.I am trying to build an Android Project which is connected to an online MySQL Server using PHP to pass the data from Android device to the online Server.I have two tables one of which contains the login details of the employee i.e.EMPLOYEE_DATA,
and the 2nd table contains the respective employee details i.e.EMPLOYEE_DETAILS.
So, I have 2 PHP files : the 1st PHP script is for the Login of the employee which runs when the employee clicks on the Login Button.This script needs to get the username of the employee which is logged in and pass it to the second PHP file.The 2nd PHP script is used for inserting the details of the respective employees. Now I need to pass the username from the 1st PHP scriptto the 2nd PHP script and insert that username to the "USERNAME" column of the table "EMPLOYEE_DETAILS". Can anyone please suggest how to proceed. I have tried using $_SESSIONS but am not getting the desired result. I have been stuck with this for a long time. Any help would be appreciated.
Edit 1 :
Login.php
<?php
$_SESSION['user_mobile'] = $_POST["mobile_num"];
$_SESSION['user_pass'] = $_POST["password"];
require "conn.php";
$user_mobile='';
$user_pass='';
$user_mobile = $_SESSION['user_mobile'];
$user_pass = $_SESSION['user_pass'];
$mysql_qry = "select * from employee_data where mobile like '$user_mobile' and password like '$user_pass';";
$result = mysqli_query($conn,$mysql_qry);
if(mysqli_num_rows($result) > 0 ) {
$row = mysqli_fetch_assoc($result);
$_POST['user'] = $name;
$name= $row["name"];
echo "Login successful.<br /> Welcome " ;
echo $name;
$insertName = "INSERT INTO employee_details(name) VALUES ('$name');";
$resultName = mysqli_query($conn,$insertName);
$shift = "SELECT CASE WHEN CURTIME() < 7 THEN 'Morning'
WHEN CURTIME() < 12 THEN 'Afternoon'
WHEN CURTIME() <17 THEN 'Evening'
ELSE 'Night'
END;";
$insertShift = "insert into employee_details(shift) values ('$shift');";
$resultShift = mysqli_query($conn,$insertShift);
}
else{
echo "Login failed.";
}
?>
I want to pass the $name variable to another PHP Script which will be used to insert the details of the user which is currently logged in.So, the username of the Employee will come from the $name variable and the other details will be the input from the Android device.
Edit 2:
Insert.php
<?php
require "conn.php";
include "login.php";
echo $_POST['user'];
$enquiry = $_POST["enquiry"];
$retail = $_POST["retail"];
$collection = $_POST["collection"];
$booking = $_POST["booking"];
$evaluation = $_POST["evaluation"];
$test_drive = $_POST["test_drive"];
$home_visit = $_POST["home_visit"];
date_default_timezone_set('Asia/Kolkata');
$IST = date('d-m-Y H:i');
$mysql_qry1 = "INSERT INTO employee_details(enquiry,retail,
collection,booking, evaluation, test_drive, home_visit, date, time) values ('$enquiry','$retail','$collection','$booking','$evaluation','$test_drive',
'$home_visit',CURDATE(),CURTIME());";
$shift = "SELECT TIME(CURRENT_TIME),
CASE WHEN TIME(CURRENT_TIME) BETWEEN '01:00:00' AND '07:00:00' THEN 'Morning'
WHEN TIME(CURRENT_TIME) BETWEEN '08:00:00' AND '13:00:00' THEN 'Afternoon'
WHEN TIME(CURRENT_TIME) BETWEEN '13:00:00' AND '18:00:00' THEN 'Evening'
END;";
$mysql_qry2 = "INSERT INTO employee_details(shift) values ('$shift');";
$ins = mysqli_query($conn,$mysql_qry2);
if($conn->query($mysql_qry1) === TRUE)
echo "Your details has been successfully inserted.";
else
echo "Error: " .$mysql_qry1. "<br>" . $conn->error;
$conn->close();
?>
I want to pass the $name variable from "Login.php" to "Insert.php" and insert into the "USERNAME" column in employee_details.
you are defining $_POST['user'] = $name before the declare $name variable so try this
login.php
<?php
require "conn.php";
$user_mobile= $_POST["mobile_num"];
$user_pass = $_POST["password"];;
$mysql_qry = "SELECT * FROM employee_data WHERE mobile = '$user_mobile' AND password ='$user_pass'";
$result = mysqli_query($conn,$mysql_qry);
if(mysqli_num_rows($result) > 0 ) {
$row = mysqli_fetch_assoc($result);
$name= $row["name"];
$_POST['user'] = $name;
echo "Login successful.<br /> Welcome",$name ;
// Your rest of code
?>
Just try to show echo $_POST['user'] in in insert.php .
for better understandig please see this
PHP Pass variable to next page
Suggestion please use prepare statement instead of direct
mysqli_query function. Why are you using like in SQL query for
matching password instead of = for login to user.
I made a login system and it works but I also have the user's session in the page after login.
How can I get user data from my database by referencing the user's session, and how can I update it when the user put what they going to change
<?php
session_start();
$query=mysql_query("SELECT username, email, password FROM user WHERE username = $_SESSION['username']");
while($row = mysql_fetch_array( $query )) {
echo .row['username'];
echo .row['email'];
echo .row ['password'];
?>
In new codes you should be using mysqli if your PHP version is high enough (if not... update...)
But here you go:
<?php
session_start();
$query = mysql_query("SELECT * FROM user WHERE username = {$_SESSION['username']}");
$row = mysql_fetch_assoc($query);
echo $row['username'];
echo $row['email'];
echo $row ['password'];
?>
Oh and the update:
<?php
$query = mysql_query("UPDATE user SET username={$_POST['username']}, email={$_POST['email']}, password={$_POST['password']} WHERE username={$_SESSION['username']}");
?>
And you should make a form for that..
The answer of Cris is almost perfect and well explained. But it is missing the quotes for the string in the statement.
As the col username will most likely be a varchar of any length.
$query = mysql_query("SELECT * FROM user WHERE username = '{$_SESSION['username']}'");
I have a html page that sends value pass to log.php :
<?php
$PHONE = $_POST['pass'];
mysql_connect('host.com', 'user', 'pass'); mysql_select_db('userdb');
$results = mysql_query(sprintf("SELECT FNAME,LNAME FROM `details` WHERE PASS='$PASS'",
mysql_real_escape_string($PASS))) or die(mysql_error()); while($row = mysql_fetch_assoc($results))
{ $rows[1] = $row; }
echo "Welcome ";
echo "$rows[1][FNAME]." ".$rows[1][LNAME]"
echo " you are logged in successfully"
?>
I get the result in log.php file in this form when value 'pass' is not being registered in database:
Welcome [//here user's first & last name if registered or remains blank] you are logged in successfully
I have table 'details' created
FNAME-First name
LNAME-Lastname
BIRTHDAY-Birthday
PHONE- user's no.
PASS- user's password.
My Question is , I want to just display ' You are not registered user' when pass value is not been registered in the database.Any help would be greatfull ?
Just check if you actually get a record or not using mysql_num_rows,
$results = mysql_query(sprintf("SELECT FNAME,LNAME FROM `details` WHERE PASS='$PASS'",
mysql_real_escape_string($PASS))) or die(mysql_error());
if(mysql_num_rows($results) > 0){
while($row = mysql_fetch_assoc($results))
{
$rows[1] = $row;
}
echo "Welcome ";
echo $rows[1]['FNAME']." ".$rows[1]['LNAME'];
echo "You are logged in successfully";
}
else{
//Redirect them back with error message
header("Location: http://www.example.com/index.php?err='You are not registered user'");
}
Note: Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
You took value in $phone variable for password and in select query used PASS= $PASS so how can it run?
Change this then run like below:
$results = mysql_query(sprintf("SELECT FNAME,LNAME FROM `details` WHERE PASS='$PHONE'",
mysql_real_escape_string($PHONE))) or die(mysql_error());
if(empty($rows[1][FNAME])){
echo 'You are not registered user';
}
else{
echo "Welcome ";
echo "$rows[1][FNAME]." ".$rows[1][LNAME]";
echo " you are logged in successfully";
}
I'm trying out my hand at php at the moment - I'm very new to it!
I was wondering how you would go about selecting all items from a mySQL table (Using a SELECT * FROM .... query) to put all data into an array but then not displaying the data in a table form. Instead, using the extracted data in different areas of a web page.
For example:
I would like the name, DOB and favorite fruit to appear in one area where there is already say 'SAINSBURYS' section hardcoded into the page. Then further down the next row that is applicable to 'ASDA' to appear below that.
I searched both here and google and cant seem to find an answer to my strange questions! Would this involve running the query multiple times filtering out the sainsburies data and the asda data where ever I wanted to place the relevant
echo $row['name']." ";
echo $row['DOB']." "; etc etc
next to where it should go?
I have got php to include data into an array (I think?!)
$query = "SELECT * FROM people";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
while($row = mysql_fetch_assoc($result))
{
echo $row['name']." ";
echo $row['DOB']." ";
echo $row['Fruit']." ";
}
?>
Just place this (or whatever your trying to display):
echo $row['name']." ";
Anywhere you want the info to appear. You can place it within HTML if you want, just open new php tags.
<h1>This is a the name <?php echo $row['name']." ";?></h1>
If you want to access your data later outside the while-loop, you have to store it elsewhere.
You could for example create a class + array and store the data in there.
class User {
public $name, $DOB, $Fruit;
}
$users = new array();
$query = "SELECT * FROM people";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
$user = new User;
$user->name = $row["name"];
$user->DOB = $row["DOB"];
$user->Fruit = $row["Fruit"];
$users[$row["name"]] = $user;
}
Now you can access the user-data this way:
$users["USERNAME"]->name
$users["USERNAME"]->DOB
$users["USERNAME"]->Fruit