mysql inner join query where id is obtained from $_GET['id'] - php

I have two tables firm and contactdetails. I am trying to get the the firm name from firm and certain contact details from contactdetails. I am using $id =$_GET['id']; to get the id . In contactdetails i have fk_firm_id which is my foreign key. I am not sure how to use the inner join query. I am trying the following query:
$sql="SELECT firm.`name` ,`address_physical_line_1` , `fax_1` , `phone_1`
FROM contactdetails JOIN firm ON contactdetails.fk_firm_id='$id'";
echo $sql;
$result = mysql_query($sql);
but i am not getting the correct firm. Can anyone help me with this query, please.

You should use like this JOIN firm ON contactdetails.fk_firm_id = firm.id
$sql=" SELECT firm.`name` ,`address_physical_line_1` , `fax_1` , `phone_1`
FROM contactdetails
JOIN firm ON contactdetails.fk_firm_id = firm.id
WHERE contactdetails.fk_firm_id = '$id'
";
$result = mysql_query($sql);

This is assuming that your firm table has a primary key called id
$sql="SELECT firm.`name` ,`address_physical_line_1` , `fax_1` , `phone_1` FROM contactdetails JOIN firm ON `contactdetails`.`fk_firm_id`=`firm`.`id`
WHERE `firm`.`id` = '$id'";
echo $sql;
$result = mysql_query($sql);

There is a mistake about JOIN and WHERE statements:
$sql = "SELECT
f.name,
c.address_physical_line_1,
c.fax_1,
c.phone_1
FROM
contactdetails c JOIN firm f ON c.fk_firm_id= f.id
WHERE c.id = '$id'";

$sql="SELECT firm.`name` ,`address_physical_line_1` , `fax_1` , `phone_1` FROM
contactdetails JOIN firm ON contactdetails.fk_firm_id=firm.id where
contactdetails.fk_firm_id='$id'";
you should join on a firm's field such as firm.id
syntax: FROM table1 LEFT JOIN table2 ON table1.field1 compopr
table2.field2 compopr is : "=","<",">","<=",">=","<>"

You are missing the WHERE clause that limits the result set to only the firm you're interested in; now you're getting all firms joined with a single contact details record.
.. where firm.id=$id
For new applications, please use a database API that has prepared statements, like mysqli or pdo.

Use the following query for inner join
$sql="SELECT firm.name ,address_physical_line_1 , fax_1 , phone_1 FROM
contactctdetails INNER JOIN firm ON contactdetails.fk_firm_id=$id";

Related

How to JOIN MySQL tables with specific ID of each table with PHP

I don't know how to join tables and fetch informations from each of them. I have clients, who has made reservations and when I click on specific client I need not only information about him from clients table, but also need information related to him by id from reservations table.
This is my ER diagram for better database understanding:
In the overview table of all reservations, based on its status I am using this query:
<?php
$query = $conn->query("SELECT * FROM `reservations` NATURAL JOIN `clients` NATURAL JOIN `houses` WHERE `status` = 'Pending' ORDER BY firstName")
or die(mysqli_error());
hile($fetch = $query->fetch_array()){
?>
It works perfectly fine, but now I need to display the reservations related only to the specific user I clicked on via link based on ID, example:
<td> <a href = "./Client-Detail.php?client_id=<?php echo $fetch['client_id']?>">
<?php echo $fetch['firstName']." ".$fetch['lastName']?></a></td>
I am not quite sure the right way how to display the reservation data of specific client.
You need to put the client_id check in the WHERE clause instead of checking status.
$stmt = $conn->prepare("
SELECT *
FROM `reservations`
NATURAL JOIN `clients`
NATURAL JOIN `houses`
WHERE client_id = ?");
$stmt->bind_param("i", $_GET['client_id']);
$query = $stmt->execute();
while ($fetch = $query->fetch_array()) {
...
}
SELECT r.*, c.*, h.*
FROM `reservations` r
LEFT JOIN `clients` c ON (c.client_id = r.client_id)
LEFT JOIN `houses` h ON (h.house_id = r.house_id)
WHERE r.status = 'Pending' AND c.client_id = '$0'
ORDER BY c.firstName ASC
This should work and bind ID via PDO or MySQLi

Postgres INNER JOIN query issue

I have two tables user and discovery but i'm unable to query them using join in postgres and php. My query is
$query = 'SELECT discovery.id,user.avatar,user.name,user.city,user.country,
discovery.image,discovery.likes,discovery.pincount FROM discovery
WHERE (discovery.id=$1)
INNER JOIN user ON (discovery.user=user.id)';
$res = pg_query_params($query, array($_POST['discovery_id']));
I am getting error
syntax error at or near "."
I have a field user in discovery table and also a table named user.
Please try this
$query = 'SELECT d.id, u.avatar, u.name, u.city, u.country,
d.image, d.likes, d.pincount FROM "discovery" AS d
INNER JOIN "user" AS u ON (d.user = u.id) WHERE (d.id = $id)';
$res = pg_query_params($query, array($_POST['discovery_id']));
The issue occured due to user is a reserved word on postgressql (postgresql.org/docs/8.1/static/sql-keywords-appendix.html), The table name user without " is referred as a keyword
Try this query
$query = "SELECT discovery.id,user.avatar,user.name,user.city,user.country,
discovery.image,discovery.likes,discovery.pincount FROM discovery d1
INNER JOIN user d2 ON d1.user=d2.id WHERE d1.id='1'";

assign sql results with same name to array

I have a general question regarding the assigning of sql results to a arrays.
What should I do when I want to assign some results to an array and when I am joining two or more tables and some columns got the same name:
Example:
$sqlExample = "select u.first_name, o.first_name from tbl_user u join tbl_owner o on u.user_id = o.user_id where u.user_id = $user_id;";
...
$userFirstName[$var] = $result['first_name'];
$ownerFirstName[$var] =
I know, that this is not a great example, but I hope that you are understanding my question..
I thought I could use something like the table prefix for the results, but it didn't worked.
-- Just an example not the code I am using/
Alias your columns in the results:
select u.first_name as user_first_name, o.first_name as owner_first_name from ...
Then use those aliases in your code:
$userFirstName[$var] = $result['user_first_name'];
$ownerFirstName[$var] = $result['owner_first_name'];
$sqlExample = "SELECT
u.first_name AS user_first_name,
o.first_name AS owner_first_name
FROM tbl_user u
JOIN tbl_owner o ON u.user_id = o.user_id
WHERE u.user_id = $user_id";
$result['user_first_name'];
$result['owner_first_name'];

how to join with tables?

First I want to write a join query to get every doc and the subjects that he uploaded i used this query but it is not working well and showing all the subjects under one doctor and all i have to get the subjects of the course is the course ID
SELECT users.ID
, fullname
, Sub_ID
, Sub_name
, Sub_ext
, Sub_path
, subject.created_at
FROM users
JOIN subject
ON users.ID = subject.ID
WHERE C_ID = '$C_ID'
Your query is wrong...
Correct is...
"SELECT u.ID,u.fullname,s.Sub_id,s.Sub_name,s.Sub_ext,s.Sub_path,s.Created_at
FROM users u LEFT JOIN subject s
ON u.ID=s.ID
WHERE s.C_ID='.$C_ID.'";
i solved it by using 2 queries
first one to get doc in that course and the second one from the subjects that doctor give
$query = "SELECT `doc-course`.ID,fullname
FROM `doc-course`
JOIN users ON `users`.`ID` = `doc-course`.`ID`
WHERE C_ID='$C_ID' ";
the second
$query2 = "SELECT Sub_ID,Sub_name,Sub_ext,Sub_path,`subject`.`created_at`
FROM subject
WHERE `subject`.`ID` = '$ID[$x]'
";

fetching data from the inner join query

hello please help me out regarding this query ,I am fetching data from different table The problem i am facing is that in the table there are similar colum name like employee have and user has also name . The query work perfectly but i am wordering about how i can display this data as
$data["employee.name"]
$data["user.name"]
here is the query:
SELECT task.employee_id , task.user_id , task.service_id, user.name,
user.pic_path , employee.name ,employee.pic_path
FROM task
INNER JOIN employee ON employee.pno = task.employee_id
INNER JOIN user ON user.pno = task.user_id
INNER JOIN service ON service.service_id = task.service_id ";
SELECT user.name AS username, employee.name AS employeename
You get the point.
There are two steps:
You need to define a column alias for at least one of the two columns in the SQL statement:
SELECT t.employee_id,
t.user_id,
t.service_id,
u.name AS user_name,
u.pic_path,
e.name AS employee_name,
e.pic_path
FROM TASK t
JOIN EMPLOYEE e ON e.pno = t.employee_id
JOIN USER u ON ur.pno = t.user_id
JOIN SERVICE s ON s.service_id = t.service_id
Then you need to update the PHP logic to use the column aliases:
$empname = $data["employee_name"];
$username = $data["user_name"];

Categories