Displaying the users information from the database - php

I am trying to created a way to call information from the database for a user to view. Such as they log in and it has their registered information viewed. I have this
session_start();
if($_SESSION['id'])
$result = mysql_query("SELECT * FROM User WHERE `id` = $_SESSION[id]")
or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
echo '<b>First Name:</b>' .$row['fname'];
echo '<br>';
echo '<b>Last Name:</b>' .$row['lname'];
}
but nothing shows up. My database name is megan, table is user, fields i want displayed are first name (fname) and last name (lname).
Can someone point me in the right direction. Thank you in advance!

Bad array indexing.
$result = mysql_query("SELECT * FROM User WHERE `id` = " . $_SESSION['id'])
You should turn on PHP error displaying.

First change this,
if(isset($_SESSION['id']))
because you have to check if the session isset correctly then do the query,
then the sql change to this,
$result = mysql_query("SELECT * FROM User WHERE `id` = ".$_SESSION['id'])

Related

how can I select and display all the rows from the same user, underneath each other with php

Users can add their workouts into a database. There will have more than one workout per user. How can i display the workout names for the logged in user, underneath each other? I have no problem adding the workouts. When I echo the names of the added workouts, it display right next to each other like "workout1workout2". I want to be able to display the workouts underneath each other. I have no idea how to do it. It would be best if I could display each workout as a button.
$query = "SELECT * FROM info WHERE username='$_SESSION[username]' ";
$results = mysqli_query($db, $query);
while($data = mysqli_fetch_array($results)) {
echo $data['workout'];
}
Firstly, you shouldn't put that username in query like that. Look into SQL Injection and how to avoid that in PHP.
As for displaying buttons/workouts. You can embed HTML code inside the echo statement like below. You can add any html like that and it will render HTML.
$query = "SELECT * FROM info WHERE username='$_SESSION[username]' ";
$results = mysqli_query($db, $query);
while($data = mysqli_fetch_array($results)) {
echo "<button>$data['workout']</button></br>";
}
$query = "SELECT * FROM info WHERE username='$_SESSION[username]' Group By username";

SQL Query and PHP checking if a user is an Admin

Basically, I have been having some trouble with sending a request to a MySQL server and receiving the data back and checking if a user is an Admin or just a User.
Admin = 1
User = 0
<?php
$checkAdminQuery = "SELECT * FROM `users` WHERE `admin`";
$checkAdmin = $checkAdminQuery
mysqli_query = $checkAdmin;
if ($checkAdmin == 1) {
echo '<h1>Working!</h1>';
}else {
echo '<h1>Not working!</h1>';
}
?>
Sorry that this may not be as much info needed, I am currently new to Stack Overflow.
Firstly, your SQL query is wrong
SELECT * FROM `users` WHERE `admin`
It's missing the rest of the WHERE clause
SELECT * FROM `users` WHERE `admin` = 1
Then you're going to need fetch the result from the query results. You're not even running the query
$resultSet = mysqli_query($checkAdminQuery)
Then from there, you'll want to extract the value.
while($row = mysqli_fetch_assoc($resultSet))
{
//do stuff
}
These are the initial problems I see, I'll continue to analyze and find more if needed.
See the documentation here
http://php.net/manual/en/book.mysqli.php
You need to have something like user id if you want to check someone in database. For example if you have user id stored in session
<?php
// 1. start session
session_start();
// 2. connect to db
$link = mysqli_connect('host', 'user', 'pass', 'database');
// 3. get user
$checkAdminQuery = mysqli_query($link, "SELECT * FROM `users` WHERE `id_user` = " . $_SESSION['id_user'] );
// 4. fetch from result
$result = mysqli_fetch_assoc($checkAdminQuery);
// 5. if column in database is called admin test it like this
if ($result['admin'] == 1) {
echo '<h1>Is admin!</h1>';
}else {
echo '<h1>Not working!</h1>';
}
?>
// get all admin users (assumes database already connected)
$rtn = array();
$checkAdminQuery = "SELECT * FROM `users` WHERE `admin`=1";
$result = mysqli_query($dbcon,$checkAdminQuery) or die(mysqli_error($dbconn));
while($row = mysqli_fetch_array($result)){
$rtn[] = $row;
}
$checkAdminQuery = "SELECT * FROM `users` WHERE `admin`"; !!!!
where what ? you need to specify where job = 'admin' or where name ='admin'
you need to specify the column name where you are adding the admin string

Using MYSQL to call a row from a table

I am trying to show specific information for my logged in users, i can call the user_name, but i have been able also to call info from the "website" entry which holds the html for the page, i need to be able to call "website" to each logged in user, i.e Susan (user_name) has the html showing when she logs in, BUT Barry (user_name) has the same html showing as susan, as it is being called from Susans row! Please help it is driving me mad!
Here is the code that is on the myaccount.php:
Welcome To <?php echo $_SESSION['user_name'];?>'s Account Page</strong>
<?php
if (isset($_GET['msg'])) {
echo "<div class=\"error\">$_GET[msg]</div>";
}
?>
<?php
$data = mysql_query("SELECT * FROM users") or die(mysql_error());
$info = mysql_fetch_array( $data );
Print "<b></b> ".$info['$website'] . " ";
?>
You have to add a WHERE clause to your query. If you have a field in your table called user_name it will be like this :
"SELECT * FROM users WHERE user_name = '" . $_SESSION['user_name'] . "'";
Note : You are using a deprecated (mysql_query) you should use rather (mysqli_query) which has different syntax
http://php.net/manual/en/mysqli.query.php
Have you tried
$data = mysql_query("SELECT * FROM users WHERE username='$_SESSION[user_name]'") or die(mysql_error());

No results showing for mysql select all

Hello guys i am trying to show all the users pokemon were the table belongsto = there username here is my code
i have a connect on top of this has well
// Get all the data from the "example" table
$result = "SELECT * FROM user_pokemon WHERE
belongsto='".$_SESSION['username']."'
AND (slot='0')'";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo $row['pokemon'];
echo $row['id'];
}
i have print red the username and there username is in the username session .
i think i mite be missing a ' or something i add or mysql at the end of the query but then the pages dies with no error
You are not running the query and have an error in it. And you're not escaping strings going into query.
A proper version of the code would be
// escape a string going to query.
$username = mysql_real_escape_string($_SESSION['username']);
// create a query
$sql = "SELECT * FROM user_pokemon WHERE belongsto='$username' AND slot=0";
// run a query and output possible error for debugging purposes.
$res = mysql_query($sql) or trigger_error(mysql_error()." in ".$sql);
// keep getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo $row['pokemon'];
echo $row['id'];
}
It appears to me that the final query would be:
SELECT * FROM user_pokemon WHERE belongsto='NAME' AND (slot='0')'
where NAME is the name you pass in. If that is the case, there is an extra single quote at the end. I presume you are getting a SQL error?

Write an ID into session as a variable

I need to insert an ID into session for later use. Table contains ID, username and password.
To get the ID im using:
$sql = "SELECT id FROM members WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
And trying to store it into session:
$_SESSION["id"] = $result;
When im trying to insert $_SESSION[id] into a table I get the value of 0 (which is the default value I made).
Kinda new on PHP, any help would be appriciated :)
you need fetch the value aswell:
$result=mysql_query($sql);
$fetch =mysql_fetch_array($result);
$_SESSION["id"] = $fetch["ID"];
Attempting to print $result won't allow access to information in the resource.
You need to use a function to access the resource returned:
$row = mysql_fetch_assoc($result)
$id = $row['id'];
The proper way to handle result would be to first check it has any value at all, i.e.:
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
mysql_query() returns a resource on success, or FALSE on error. You need to fetch the rows from that result:
$result = mysql_query("SELECT id, name FROM mytable") or die(mysql_error());
$row = mysql_fetch_array($result);
if($row){ // check that there was really selected at least one row
$_SESSION['id'] = $row['id'];
}

Categories