display user profile from mysql database with php sessions [duplicate] - php

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.

Related

While in other while using fetch_array() in PHP

Can't find solution for this so i'm posting new question. As thread name say, i want to use while in other while when using fetch_array in PHP. I want to display info about user in every post.
My example code:
<?php
$users_query = "SELECT * FROM users";
$users_result = mysqli_query($db, $users_query);
$posts_query = "SELECT * FROM posts";
$posts_result = mysqli_query($db, $posts_query);
while($row1 = $posts_result->fetch_array()) {
echo '<div class="post"><div class="user-info">';
while($row2 = $users_result->fetch_array()) {
echo $row2["user_name"]." ".$row2["user-posts-count"];
}
echo '</div>'.$row1["message"].'</div>';
}
?>
It's just example but showing what i want. Second while executed only once, so info about user in post is displayed only in first post. Any solution?
Thank you and sorry for my bad english, Adrian.

Getting user data from session

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']}'");

PHP help: Follow System in Microblogging

I'm making a site similar to Instagram. I am very new to php. I created a follow button in the user's profile.
How do you make the follow button disappear when you already followed the user?
How do you replace it with unfollow button?
// my php code for following
if (isset($_POST['addfriend'])){
$fromuser = $user;
$touser = $username;
if($fromuser == $username){
$Msg = "You cannot follow yourself<br/>";
}
else
{
$getID= mysql_query("SELECT userID FROM user WHERE username='$user'");
$get_ID_row = mysql_fetch_assoc($getID);
$ID_db = $get_ID_row['userID'];
$sql = "insert into following (userID, fromUser, toUser)
values ('$ID_db','$fromuser', '$touser')";
$result = mysql_query($sql);
$Msg= "Success! <br/>";
}
}
else{
//Do nothing
}
//my code for the follow button
<form action="<?php $user;?>" method ="POST">
<?php echo $Msg; ?>
<input type = "submit" name ="addfriend" value = "Follow"/>
</form>
On the page where you are going to show the Follow or Unfollow button, first run a MySQL query to find out if you are already following the person:
$sql = "select * from following
where userID = $user
and fromUser = $fromUser
and toUser = $toUser";
$result = mysql_query($sql);
if( $result) {
if( mysql_num_rows($result) > 0) {
// if we get here we know we are already following that person
....[see below]
Now dynamically create whichever button you need:-
if( mysql_num_rows($result) > 0) {
// if we get here we know we are already following that person
echo '<input type = "submit" name ="removefriend" value = "Un-follow"/>';
}
else
{
echo '<input type = "submit" name ="addfriend" value = "Follow"/>';
}
And on the following page where you are getting the form results, check for both buttons:
if (isset($_POST['addfriend'])) {
...[do what you already have]
}
else
if (isset($_POST['removefriend'])) {
...[do SQL to remove the record from the following table]
}
Please be aware also that as of PHP v5.5 this style of MySQL is deprecated. At some stage in the future you will have to convert your programs to the MySQLi or PDO_MySQL extensions, before they eventually discontinue support. See the PHP manual about this at eg http://php.net/manual/en/mysqlinfo.api.choosing.php.
Would be easier with OO PHP. However, if you chose procedural, let's assume we have a table of friends. Which keeps the id of each of my friends.
e.g.: Smith follows John
Then you do something like
$following = mysql_query("SELECT COUNT(*) FROM followers WHERE followerid = ".$_SESSION['id']." AND followeeid = ".$username);
Check if You follow the person already:
if($following){//$following == true
}

Load data from multiple columns in Joomla

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

How to select certain fields from table in mySQL using PHP

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

Categories