In my data base I have 3 tables.
user (table name)
so many row are there one of the row name user and if
--------------------
| id | user |
--------------------
| 7 | user |
| 8 | user_name_2 |
| 11 | user_name_5 |
--------------------
and another table call data
----------------------------
| id | user-id | number |
----------------------------
| 1 | 7 | 789654125 |
| 2 | 8 | 465654545 |
| 3 | 11 | 884554511 |
----------------------------
In table td user_id is table user id
now I want to show name and number in php
$conn = mysqli_connect("localhost","root","QAZWS12","user");
$sql = "SELECT * FROM user"
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($result)) {
$id = $row["id"];
}
did the same and get $number = $row["number"]
Now I want is in data table user id auto get table user number how can i do that?
Final output
user 789654125
user_name_2 465654545 like that
You have to use join here. For more details on different types JOINS refer either official MySQL doc or search online
replace this query
$sql = "SELECT u.id,u.user AS user_name,d.number AS user_number
FROM user u LEFT JOIN description d ON u.id = d.user-id";
The above query will fetch user id and username and user number
replace this too
$id = $row["id"].' '.$row['user_name'].' '.$row['user_number'].'<br/>';
This outputs as
7 user 789654125
Try below SQL query with LEFT JOIN to get the username with their mobile number in the result set:
$sql = "SELECT u.user, d.number
FROM user as u
LEFT JOIN data as d ON d.user-id = u.id
"
user SQL Left join
$sql = "SELECT * FROM user LEFT JOIN call_data ON user.id = call_data.user-id";
while accessing data
$number = $row['number'];
learn about SQL JOINS TUTORIAL
Related
I have a 2 tables:
Category with Primary Key ID and column Name
Employee with Primary Key ID and column Category_id
Note: Category_id now displays ID correctly
I want to show Name instead of ID for output from Employee.
Attempt:
$categ = mysql_query("SELECT * FROM employee WHERE id = '" . $_GET['id'] . "'");
$rows = array();
while ($row = mysql_fetch_assoc($categ)) {
$website_cat = $row;
}
Category Table:
+----+----------------+
| ID | Name |
+----+----------------+
| 23 | Manager |
| 10 | Boss |
| 14 | Worker |
| 41 | Another |
+----+----------------+
Employee Table:
+----+----------------+
| ID | Category_id |
+----+----------------+
| 1 | Manager |
| 2 | Boss |
| 3 | Worker |
| 4 | Another |
+----+----------------+
Output:
echo $website_cat['category_id'];
The SQL keyword you're looking for is JOIN. Your query could be something like this:
SELECT * FROM employee INNER JOIN category ON employee.category_id = category.id WHERE id = ...
Or, more readably:
SELECT
*
FROM
employee
INNER JOIN category
ON employee.category_id = category.id
WHERE
id = ...
(Note: I removed that last bit of the WHERE clause on purpose because I'm not comfortable putting SQL injection vulnerabilities in an answer. Please read this to learn some of the basics of properly executing SQL queries involving user input. Currently your code is wide open to a very common form of attack.)
Since some of your columns share the same name, you may even want to more explicitly request them:
SELECT
employee.id AS employee_id,
category.id AS category_id,
category.name AS category_name
FROM
employee
INNER JOIN category
ON employee.category_id = category.id
WHERE
id = ...
Then in your code you'd have access to these fields:
employee_id, category_id, category_name
So you could output the value you want:
echo $website_cat['category_name'];
You need to join category table
$categ = mysql_query("
SELECT employee.*, category.name as category_name FROM employee
INNER JOIN category on category.id = employee.category_id
WHERE id = '" . $_GET['id'] . "'");
Then output with $website_cat['category_name']
I am trying to inner join 3 tables that is from OS TICKET Database.
The code I am using is $qry = "SELECT qbcd_user_email.address, qbcd_user_email.user_id FROM qbcd_user_email INNER JOIN qbcd_user ON qbcd_user.id = qbcd_user_email.user_id INNER JOIN qbcd_ticket ON qbcd_ticket.user_id WHERE (qbcd_user_email.address = '.$email.') ORDER BY qbcd_ticket.ticket_id DESC";
Code is returning:
string(287) "SELECT qbcd_user_email.address, qbcd_user_email.user_id FROM qbcd_user_email INNER JOIN qbcd_user ON qbcd_user.id = qbcd_user_email.user_id INNER JOIN qbcd_ticket ON qbcd_ticket.user_id WHERE (qbcd_user_email.address = '.patrick.kershner#gmail.com.') ORDER BY qbcd_ticket.ticket_id DESC"
but it is not displaying anything in the while clause:
while ($row = mysqli_fetch_assoc($result)){
echo $row['qbcd_ticket.number]."<br>";}
I am not sure what is going on, or why its not displaying the results.
Can someone check out my code above and verify?
Try to add the number to your selected properties
$qry = "SELECT qbcd_user_email.address, qbcd_user_email.user_id, qbcd_ticket.number FROM qbcd_user_email INNER JOIN qbcd_user ON qbcd_user.id = qbcd_user_email.user_id INNER JOIN qbcd_ticket ON qbcd_ticket.user_id WHERE (qbcd_user_email.address = '.$email.') ORDER BY qbcd_ticket.ticket_id DESC"
the first table is:
qbcd_ticket:
rows:
ticket_id | number | user_id | user_email_id | status_id | dept_id | and more...
5 | 762086| 2 | 0 | 1| 1 |
the next is qbcd_user_email
rows:
id | user_id | flags | address
2 | 2 | 0 | example#demo.com
the last is: qbcd_user
id | org_id | default_email_id | status | name | created | updated
2 | 0 | 2 | 0 | Patrick Kershner | 2017-03-03 10:44:28 | 2017-03-03 10:44:28
The information that I need to display, is all corresponding Tickets associated with the customer where it = the email address.
the only static variable that will not change is $_SESSION['user_email']; which is logged by logging into the members area.
i have 2 tables
here is table 1 table name is
forexample
itemlist
+-----+----------+-----+
| uid | username | age |
+-----+----------+-----+
| 1 | doe | 17 |
| 2 | smith | 18 |
| 3 | john | 30 |
+-----+----------+-----+
and other one is
fav
+-----+------+---------+
| uid | user | itemuid |
+-----+------+---------+
| 1 | alex | 2 |
+-----+------+---------+
Here is my mysql query *NOT Working * any way to fix this problem when i run php file i got error in mysql syntax
SELECT c.uid, c.username, c.age,i.uid,i.user,i.itemuid
from itemlist c
left join fav i on c.uid = i.itemuid
if (i.user = 'alex') THEN
SET #fav = 1;
ELSE
SET #fav = 0;
END IF
this is sample php
while ($row = mysqli_fetch_array($res)){
if ($row['fav'] = '1'){
echo $row['username']." is exit in fav";
}else{
echo $row['username']." is not exit in fav";
}
}
i hope you understand my question right ?
To get a column named fav returned in the resultset, you would need to include an expression in the SELECT list, and give it an alias fav.
It's not at all clear why you would need a MySQL user-defined variable; if you don't know why you'd need one, then you probably don't need one.
Given that your PHP code is looking for a column named fav in the resultset, likely you want something like this:
SELECT c.uid
, c.username
, c.age
, i.uid AS i_uid
, i.user
, i.itemuid
, IF(i.user='alex',1,0) AS fav
FROM itemlist c
LEFT
JOIN fav i ON i.itemuid = c.uid
Note that the original query had two columns named uid; if you want to return both, and be able to reference both of those by column name, you need to have distinct names for each. In the query above, I've assigned an alias to the i.uid column so that both uid columns will be available by distinct column name.
I have two tables. The first table holds simple user data and has the columns
$username, $text, $image
(this is called "USERDATA").
The second table holds information about which users "follow" other users, which is set up with the columns
$username and $usertheyfollow
(this is called "FOLLOWS").
What I need to do is display the data individually to each user so that it is relevant to them. This means that userABC for instance, needs to be able to view the $text and $image inputs for all of the users whom he/she follows. To do this, I believe I need to write a sql query that involves first checking who the logged in user is (in this case userABC), then selecting all instances of $usertheyfollow on table FOLLOWS that has the corresponding value of "userABC." I then need to go back to my USERDATA table and select $text and $image that has a corresponding value of $usertheyfollow. Then I can just display this using echo command or the like...
How would I write this SQL query? And am I even going about the database architecture the right way?
With tables like so:
userdata table
______________________________
| id | username | text | image |
|------------------------------|
| 1 | jam | text | image |
+------------------------------+
| 2 | sarah | text | image |
+------------------------------+
| 3 | tom | text | image |
+------------------------------+
follows table
_____________________
| userId | userFollow |
|---------------------|
| 1 | 2 |
+---------------------+
| 1 | 3 |
+---------------------+
and use the following SQL:
SELECT userdata.text, userdata.image FROM follows LEFT JOIN userdata ON follows.userFollow = userdata.id WHERE follows.userId = 1
will get all the text and images that user with id '1' follows
As it turns out, neither of these answers were right. #jam6459 was closest.
The correct answer is the following:
SELECT userdata.text, userdata.image, follows.userFollow
FROM userdata
LEFT JOIN follows ON follows.userFollow = userdata.username
WHERE follows.userId = $username
I also found it easier to not have a username correspond to an Id as in jam's table example. This is because the same user can have multiple entries in "USERDATA". I instead used username as the Id.
function get_text_image($username)
{
$sql = "SELECT * FROM USERDATA where username='".$username."'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo $row['text'];
echo $row['image'];
}
}
function display_data_of_followers($userid)
{
$sql = "SELECT usertheyfollow FROM follow WHERE userid = ".$userid."";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
get_text_image($row['usertheyfollow']);
}
}
display_data_of_followers($userid);
I would love to get some help with this. I'm using php and MySQL to build a website. I currently have 3 tables, I'll include less in the examples. Basically I have a users table, a groups table and a grouplink table. What I have is the uid from the users table.
How should I go about it in php so I could, let's say: match users-uid to grouplink-uid, get the grouplink-gid it matches with, match grouplink-gid to groups-gid and return groups-grpname? And goes on a while loop so all group names the user is associated with are displayed.
Thanks in advance to those who will be willing to extend a hand.
users
-------
| uid |
-------
| 1 |
-------
groups
---------------
| gid |grpname|
---------------
| 1 | grp1 |
---------------
| 2 | grp2 |
---------------
grouplink
-------------------
| glid| uid | gid |
-------------------
| 1 | 1 | 1 |
-------------------
| 2 | 1 | 2 |
-------------------
uid is fk to uid in users while gid is fk to gid in groups
That's just a simple 2-way join query:
SELECT users.uid, groups.gid, groups.grpname
FROM users
INNER JOIN grouplink ON users.uid = grouplink.uid
INNER JOIN groups ON grouplink.gid = groups.gid
the actual retrieval of a joined query result is no different than a single table query - you've just got more fields to deal with.
The SQL query that will get you what you're looking for goes something like this (assuming no null values in the grouplink table):
SELECT u.uid, g.gid, g.grpname
FROM users u
JOIN grouplink gl ON u.uid = gl.uid
JOIN groups g ON gl.gid = g.gid
Here is one way:
SELECT users.uid, groups.gid, groups.grpname
FROM users u, groups g, grouplink gl
WHERE g.id = gl.gid
AND gl.uid = u.uid
When the user-id is in the variable $iUserId you could query following sql string:
$sSql = "SELECT groups.`grpname` FROM groups
INNER JOIN grouplink ON groups.`gid` = grouplink.`gid`
WHERE grouplink.`uid` = '" . intval($iUserId) . "'";
$rRes = mysql_query($sSql);
$aGroups = array();
while (($aRow = mysql_fetch_array($rRes)) !== false) {
$aGroups[] = $aRow['grpname'];
}
Now all groups associated with the user are in the array $aGroups.