I have a web page that has a user database, each user has a unique User ID. Now I have created a login page where it starts a session, and the session includes the user ID.
I also have an "update status" option where the user types in a status and it submits the status and the users Unique ID into a new table. So the table will have the Users ID and the status that the user put in.
Now I want to display the users status on a page, and I want to display the user's Username along with it. So basically the code must take the user ID from the status table and then match it with the user ID from the Users table, and from that it must give me the username from the matching ID in the users table.
*FINAL WORKING CODE*
//Connect to mysql
mysql_connect("localhost","root","");
//connect to database
mysql_select_db("users");
//query the database
$query = "select * \n"
. " from status inner join users \n"
. " on status.user_id = users.id\n ORDER BY users.id DESC";
//fetch results / convert results into an array
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
$s_firstname = $row['firstname'];
$s_lastname = $row['lastname'];
$s_status = $row['status']
}
I recommend spending some time learning about joins in sql. You would only need one SQL query, and it would be something like this...
select *
from status inner join users
on status.userid = users.userid
where active = '1' and connect = '1'
Edit: Although using mysql functions is not recommended (see the note at the top of this page - http://uk3.php.net/mysql_query), your lines should be like this..
$query = "select * from status inner join users on status.user_id = users.id where active = '1' and connect = '1'";
$result = mysql_query($query);
WHILE ($row = mysql_fetch_array($result)){
... rest of your code....
Related
Frist I have table of general notifications this table have three field ID+Body+Date and from my app flutter any message send to users save this message in this table.
Now the problem is, the message goes to all users, so I'm trying to make a way to find out which users have seen this message from others.
Now I make another table to save userID and notificationID.So if Userid and NotificationsID in IsWatchedNotifications table this means message has been viewed and if not have data then this means message not viewed to now.
Now I have problem with that query how I can write it to work like that?
this is my query line to now:
$sql = "SELECT * FROM Notifications
LEFT JOIN IsWatchedNotifications ON Notifications.NotificationsID = IsWatchedNotifications.NotificationsID
where ";
Full code:
<?php
$UserID= $_GET['UserID'];
$sql = "SELECT * FROM Notifications
LEFT JOIN IsWatchedNotifications ON Notifications.NotificationsID = IsWatchedNotifications.NotificationsID
where ";
$stmt = $con->prepare($sql);
$stmt->bind_param("s",$UserID);
$stmt->execute();
$result = $stmt->get_result();
if ($result) {
while($row[] = $result->fetch_assoc()) {
$item = $row;
$json = json_encode($item, JSON_NUMERIC_CHECK);
}
} else {
}
echo $json;
$con->close();
?>
Anyone can help me
Thank you
assumeing that your column name is user_id
You need only to add the condition with placeholder
$sql = "SELECT * FROM Notifications
LEFT JOIN IsWatchedNotifications ON Notifications.NotificationsID = IsWatchedNotifications.NotificationsID
where user_id = ?";
I am new to PHP coding and just trying to fix some functionality on my site that was left over from the lead developer.
The site, [Vloggi], is a marketplace. So I need to show the name of the job poster in the assignments page . The table I have the jobs in only has the ID, not the name.
So I need a join, but I've tried and it breaks the entire site.
The SQL has 17 tables, I need to display the User Name (usr_name) contained in table 3, the organisation contained in table 7 (usrg_orgname) with the job posting user (vlop_usr_id) details in table 14.
The primary key is users.usr_id, which is linked to users_gor.usrg_usr_id and vlog-ops.vlog_usr_id.
Table 3: users
usr_id, usr_email, usr_password, usr_fbuser, usr_fbtoken, usr_name, usr_loc_name, usr_loc_lat1, usr_loc_lon1, usr_loc_lat2, usr_loc_lon2, usr_status, usr_gor, usr_vgr, usr_token, usr_regtoken,
table 7: users_gor
usrg_usr_id, usrg_creditops, usrg_creditvlog, usrg_creditvlogette, usrg_destination, usrg_orgname, usrg_orgtype, usrg_location, usrg_website, usrg_jobtitle, usrg_phone, usrg_address1, usrg_address2, usrg_state, usrg_postcode, usrg_country
Table 14: vlog-ops
vlop_id, vlop_title, vlop_description, vlop_tags, vlop_deadline, vlop_quantity, vlop_quantityposted, vlop_vser_id, vlop_usr_id,vlop_loc_name, vlop_loc_lat1, vlop_loc_lon1, vlop_loc_lat2, vlop_loc_lon2, vlop_campaign, vlop_rules, vlop_tips, vlop_status
So in main.php i have written the following Sql lookup
in main.php, I have the following SQL lookups:
$sql = "SELECT * FROM users_gor WHERE usrg_usr_id = ".$db->quote($user_info['usr_id'])." LIMIT 1";
$rows = $db->select($sql);
$users_gor = $rows[0];
$sql = "SELECT * FROM users_vgr WHERE usrv_usr_id = ".$db->quote($user_info['usr_id'])." LIMIT 1";
$rows = $db->select($sql);
$users_vgr = $rows[0];
$sql = "SELECT * FROM users WHERE usr_id = ".$db->quote($user_info['usr_id'])." LIMIT 1";
$rows = $db->select($sql);
$users = $rows[0];
$sql = "SELECT * FROM vlog-ops WHERE vlop_usr_id ".$db->quote($user_info['usr_id'])." LIMIT 1";
$rows = $db->select($sql);
$users = $rows[0];
$sql = "SELECT usr_name AS vlop_usr_name FROM users WHERE usr_id = ".$db->quote($user_info['usr_id'])." LIMIT 1";
$rows = $db->select($sql);
$users = $rows[0];
And then in the page template itself, I have written
<?php echo $vlop['vlop_vser_id'] ?>
<?php echo $vlop['vlop_usr_name'] ?>
The first one works, the second doesn’t. What I want eventually is to display the user name and the organisation name in a table.
Whenever I try a JOIN or a NATURAL JOIN or a LEFT JOIN it breaks and the entire site goes blank.
Any help for a newbie would be appreciated with a million thanks.
When you use JOIN you need to specify how you're joining them.
In the query below I'm assuming you're looking for the fields in bold from your question.
$query='SELECT u.usr_name, g.usrg_orgname, v.vlop_usr_id FROM users u
JOIN vlog-ops v on u.usr_id = v.vlop_usr_id
JOIN users_gor g on u.usr_id = g.usrg_usr_id';
I believe I got the name of the fields right but if not just replace them with the correct ones.
Once you have the data fetched, you just loop through the results:
$result = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($result)) {
echo 'User name = ' . $row['u.usr_name'];
echo 'Org name = ' . $row['g.usrg_orgname'];
echo 'Job posting user id = ' . $row['v.vlop_usr_id'];
}
I have two MySQL tables
users with id, user_formOfAdress and serveral additional fields. Field user_formOfAdress contains the id of table formofadress
formofadress with id, formOfAdress_german and serveral additional fields, for example id 1 = Mr., id 2 = Mrs.
The record of the users table is identified by a Session variable.
To output not the id of the field user_formOfAdress but the value of the table formofadress.formOfAdress_german (for example Mr. or Mrs.) I have written this:
if(array_key_exists("id", $_SESSION) && $_SESSION['id']){
$uid = $_SESSION['id'];
$link = mysqli_connect("localhost:3307", "root", "Dinah123", "proficrm");
$query = "
SELECT formofadress.ID AS formofadress_ID, formofadress.formOfAdress_german, users.ID, users.user_formOfAdress
FROM `formofadress`
LEFT JOIN users
ON formofadress.formofadress_ID = users.user_formOfAdress
WHERE `users.ID` = '".$uid."'
LIMIT 1
";
$result = mysqli_query($link, $query);
$record = mysqli_fetch_assoc($result);
$user_formOfAdress = $record['formOfAdress_german'];
}
"FROM formofadress" because I want to output the Mr. or Mrs. of this table, but I have to use also the users table because of the Session ID which is also the id of the users record ...
Not every record in the users table has a value in user_formOfdAdress (value 1, 2 or NULL) but every record in the formofadress table has a fixed value.
Error is:
Undefined variable: user_formOfAdress located in the last row
It's my first time to use JOINs and I'm unfortunately not able to solve this issue even after a long time of searching.
Correct code:
if(array_key_exists("id", $_SESSION) && $_SESSION['id']){
$uid = $_SESSION['id'];
$link = mysqli_connect("localhost:3307", "root", "Dinah123", "proficrm");
$query = "
SELECT formofadress.ID, formofadress.formOfAdress_german, users.ID, users.user_formOfAdress
FROM formofadress
LEFT JOIN users
ON formofadress.ID = users.user_formOfAdress
WHERE users.ID = '".$uid."'
";
$result = mysqli_query($link, $query);
$record = mysqli_fetch_assoc($result);
$user_formOfAdress = $record['formOfAdress_german'];
}
There were problems with your SQL syntax. You used LEFT JOIN to join users to formofadress while users sometimes can't follow and you will possibly get a NULL value. I also cleaned up your other query syntax so it is more readable.
Also, check to see if the database is expecting an INT for $uid. If not then return the apostrophes ''.
if(array_key_exists("id", $_SESSION) && $_SESSION['id'])
{
$uid = $_SESSION['id'];
$link = mysqli_connect("localhost:3307", "root", "Dinah123", "proficrm");
$query = "SELECT u.ID, u.user_formOfAdress, f.ID, f.formOfAdress_german,
FROM users u LEFT JOIN formofadress f ON f.formofadress_ID = u.user_formOfAdress
WHERE u.ID = ".$uid." LIMIT 1";
// if database is not expecting `INT` for `u.ID` then return the apostrophes ' '
$result = mysqli_query($link, $query);
// mysqli_fetch_assoc returns case sensitive keys
$record = mysqli_fetch_assoc($result);
$user_formOfAdress = $record['formOfAdress_german'];
}
I would like to ask how to select all from table based on username? I mean for example my user 1 insert his/her data and send to localhost and in status page will display their own data only. Below is my status page php.
<?php
require_once('dbConnect.php');
//Creating sql query
$sql = "SELECT * FROM Table";
//getting result
$r = mysqli_query($con,$sql);
//creating a blank array
$result = array();
//looping through all the records fetched
while($row = mysqli_fetch_array($r)){
//Pushing name and id in the blank array created
array_push($result,array(
"id"=>$row['id'],
"username"=>$row['username'],
"name"=>$row['name']
));
}
//Displaying the array in json format
echo json_encode(array('result'=>$result));
mysqli_close($con);
?>
"SELECT * FROM Table WHERE username = 'john'"
This is assuming your table is actually named 'Table' as you have it above. Otherwise replace it with the actual table name.
If you have a variable for user name do this:
"SELECT * FROM Table WHERE username = '" . $username . "'";
You can use WHERE clause in the query like.
"SELECT * FROM Table WHERE username = 'lun L'"
But this is not a good way to fetch data by user name. You should select users on ID base and ID should be unique. Because there are so many users who might have the same name.
"SELECT * FROM Table WHERE youUniqueColName = '$uniqueVal'"
If unique column is id then use id='$id'
SELECT * FROM tablename WHERE username = 'username'
Use this if you want a fixed SQL Query in your variable, or use;
SELECT * FROM tablename WHERE username = '$username'
if you have an input that asks a username to display a specific username..
I want affiliates to be able to see how many referrers they have.
So my table has 3 columns: id, IP, username and referee. A member can have more than 1 account.
Here is what I have so far.
//Get all of that IP's usernames
$sql = "SELECT username FROM ".MYSQLTABLE." WHERE ip = '" . $_SERVER['REMOTE_ADDR']."'";
$res = mysql_query($sql)or die(mysql_error());
//Select all usernames, who's referee = one of IP's usernames (using above query)
$sql1 = "SELECT usernames FROM ".MYSQLTABLE." WHERE referee = '".$res."'";
$res1 = mysql_query($sql1)or die(mysql_error());
while($row=mysql_fetch_array($res1))
{
You can do this is a single query which will simplify things:
SELECT DISTINCT(u.username)
FROM yourTable u
INNER JOIN yourTable r ON (r.username = u.referee AND r.ip = 'TheIP');