PHP and SQL INSERT JOIN 2 tables with criteria error - php

$rentingsquery = "SELECT * FROM TBL_rentings WHERE personId='$personId'
JOIN TBL_rentings ON TBL_properties.propertyId = TBL_rentings.propertyId
JOIN TBL_people ON TBL_rentings.personId = TBL_people.personId";
$rentingsResult=mysql_query($rentingsquery) or die ("Query to get data from TBL_properties failed: ".mysql_error());
This is the error I get when I execute the code in my web page:
What I'm trying to do is select only the 'rentings' where the personId = $personId, (as the current page is a page for each individual person), and display only those 'rentings'. Also in the code which I haven't posted I'm displaying data about the property which is related to that rent, hence why I'm trying to join the renting and properties table with propertyId so that I call the correct property's details off the database.

$rentingsquery = "SELECT * FROM TBL_rentings
JOIN TBL_rentings ON TBL_properties.propertyId = TBL_rentings.propertyId
JOIN TBL_people ON TBL_rentings.personId = TBL_people.personId
WHERE personId='$personId'";
$rentingsResult=mysql_query($rentingsquery) or die ("Query to get data from TBL_properties failed: ".mysql_error());
First of all try this, the where condition has to be after the joins.
If there are other problems I will edit the answer
The next problem is that you are joining the same table, but you are joining it in an external field. A quick fix, if I am supposing correct, is that you wrongly joined with an unwanted table:
SELECT * FROM TBL_rentings
JOIN TBL_properties ON TBL_properties.propertyId = TBL_rentings.propertyId
JOIN TBL_people ON TBL_rentings.personId = TBL_people.personId
WHERE TBL_rentings.personId='$personId'

Related

How Can I show data from 2 different tables using INNER JOIN in PHP?

I'd like to know what I can do to show data from two different tables using INNER Join in PHP. I use while to recover my database records, but I can't get data from my table "Cliente" and my table "Usuario" simultaneously, I just don't know where I'm missing.... May someone help me to solve it?
Here's the code I use:
$sql6 = "SELECT * FROM cliente
INNER JOIN usuario ON cliente.id_usuario = usuario.id_auto
WHERE
usuario.id_imobiliaria = '$id_imobiliaria'
ORDER BY cliente.id";
$resultado6 = mysql_query($sql6) or die ("Erro na consulta");
$registros1 = mysql_num_rows($resultado6);
while ($linha6 = mysql_fetch_assoc($resultado6)){
$nome_corretor = $linha6['usuario.id_auto'];
use var_dump($linha6) to see how column are named.
Other solution is not to use * to select all columns but explicit them in the select statement: e.g. SELECT usuario.id_auto as 'auto', usuario.id_imobiliaria as 'imobiliaria' etc. so you can get $linha6['auto'], etc.

Fetching all Data from Multiple tables based on Single id

I have to select data from multiple tables based on single key value. I have one table called maintable where I will get all the ids from, and i have another 10 tables in the same database which have maintable.id as a foreign key. Now I have to retrieve data from the 10 tables where maintable.id matches in one single table.
The code I have tried is:
$sql = select id from maintable;
$runsql = mysql_query($sql);
while($sqlRow = mysql_fetch_array($runsql ,MYSQL_ASSOC)) {
for($i=1;$i<=10(This is another table count);$i++) {
$servSql = "select * from table.$i where ref_id = ".$sqlRow['id'];
$runServerSql = mysql_query($servSql);
while($serverRow = mysql_fetch_array($runServSql,MYSQL_ASSOC)) {
}
}
}
Try something like this in a join:
SELECT * FROM maintable m
INNER JOIN othertable o
ON m.id = o.id
That will select from both tables using an inner join ON the id column. You may want to look up a basic SQL tutorial to learn the basic types of joins you can use. Good luck!

Selecting from two tables issue

This is was am trying to do table A has all profession categories & table B has users details.i want select from the 2 tables and match up categories selected,if user select 'web developer' from a dropdown of categories.i want to display list of users under web developer.
<?php
$q = intval($_GET['q']);
//select state of the two tables here
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)) {
?>
///
<?php }?>
it's unclear from the question and lack of database details what and how to do the sql query ( which I think is what you are actually asking for ) but something along these lines perhaps.
select * from `tableB` b
left outer join `tableA` a on a.`professionid`=b.`professionid`
where a.`profession`='Web Developer'

Mysql query working from command line but only half working in PHP

I have two tables
1.owners
id
firstname
lastname
2.product
id
id2
id3
item
SELECT * FROM owners LEFT JOIN product ON product.id=owners.id where firstname='Jezebel';
Works fine from the command line returning all relevant items from owners and product but using the following PHP
$result = mysql_query("SELECT * FROM owners LEFT JOIN product ON product.id=owners.id where firstname='".$_POST['fname']."'")
only returns results from the table owners.
I have googled extensively and don't see anyone else with this problem.
$fname= $_POST['fname'];
$result = mysql_query("SELECT * FROM owners LEFT JOIN product ON product.id=owners.id where firstname='$fname'")
First, make your query similar to this
$query = sprintf("SELECT * FROM owners LEFT JOIN product ON product.id=owners.id where firstname='%s'",mysql_real_escape_string(trim($_POST['fname'])));
$result = mysql_query($query) or die(mysql_error());
var_dump(mysql_fetch_assoc($result));
and let me know the result so I can update this answer.
First of all: You have two table which match on autoincrement column id?
However, try this?
SELECT tb1.id, tb1. firstname, tb1.lastname, tb2.id, tb2.id2, tb2.id3, tb2.item FROM owners AS tb1 LEFT JOIN product AS tb2 ON tb2.id=tb1.id where tb1.firstname=$fname
I have found the error. It was nothing to do with the select statement. I had a typo in the display code. It should have been like this
echo "</td><td>";
echo $row['id3'];
But was like this instead
echo "</td></td>";
echo $row['id3'];
Thank you all for your help.

Multiple left joins, how to output in php

I have 3 tables I need to join. The contracts table is the main table, the 'jobs' and 'companies' table are extra info that can be associated to the contracts table.
so, since I want all entries from my 'contracts' table, and the 'jobs' and 'companies' data only if it exists, I wrote the query like this....
$sql = "SELECT * FROM contracts
LEFT JOIN jobs ON contracts.job_id = jobs.id
LEFT JOIN companies ON contracts.company_id = companies.id
ORDER BY contracts.end_date";
Now how would I output this in PHP? I tried this but kept getting an undefined error "Notice: Undefined index: contracts.id"...
$sql_result = mysql_query($sql,$connection) or die ("Fail.");
if(mysql_num_rows($sql_result) > 0){
while($row = mysql_fetch_array($sql_result))
{
$contract_id = stripslashes($row['contracts.id']);
$job_number = stripslashes($row['jobs.job_number']);
$company_name = stripslashes($row['companies.name']);
?>
<tr id="<?=$contract_id?>">
<td><?=$job_number?></td>
<td><?=$company_name?></td>
</tr>
<?
}
}else{
echo "No records found";
}
Any help is appreciated.
The column names will not be prefixed like this - and with each table having a column called "id" you could be in trouble. You should explicitly identify the columns you want returned rather than using "select *", and you then just retrieve the column by name un prefixed (e.g. $row['job_number']).
The below would solve you problem.
$sql = "SELECT contracts.id AS contract_id, jobs.job_number, companies.name FROM contracts
LEFT JOIN jobs ON contracts.job_id = jobs.id
LEFT JOIN companies ON contracts.company_id = companies.id
ORDER BY contracts.end_date";
Your problem is likely to be realted to the fact you are using two tables with the field id this is why you should select them as an alias using the mysql as clause.
You may also want to look into using a naming convention for your fields and sticking with it. For example, check out the theory of Hungarian Notation, this would stop issues like this from arrissing.

Categories