PHP/MySQL INNER JOIN Triples the amount of rows? - php

I have a system where I getting images out of my database, but when it does that, there is 3x of the same images.
I have tried with different ways, DISTINCT and such, but I have no clue how I fix this.
Here is my query code:
<?php
$id = $_GET['id'];
$query = "SELECT DISTINCT * FROM billeder INNER JOIN album ON fk_album_ID = $id";
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_assoc($result))
{
$thumb_src = 'billeder/thumb_'.$row['billeder_sti'];
$full_src = 'billeder/'.$row['billeder_sti'];
echo "
<div class='ikon'>
<a href='$full_src'>
<img src='$thumb_src' alt='' />
</a>
</div>
";
}
?>
Hope someone can help me on the way to fix this :)

Without being able to see your table structure I won't be able to give an exact answer but the likely reason is because your INNER JOIN is not setup correctly.
SELECT DISTINCT *
FROM billeder
INNER JOIN album
ON (billeder.fk_album_ID = album.pk_album_ID)
WHERE
billeder.fk_album_ID = $id
Something like the above would be the correct way to JOIN a table and using a WHERE clause to then limit the date received.

JOIN must be used with two tables columns. See example:
SELECT * FROM tableA a INNER JOIN tableB b ON a.id = b.a_id;
What you're trying to make is something like this:
"SELECT DISTINCT * FROM billeder INNER JOIN album ON
billeder.fk_album_ID = album.album_id WHERE billeder.id = $id"
You shouldn't pass an argument to the JOIN. The arguments must be used on the WHERE clause.

Related

select from two mysql tables where column value is similar and id is retrieved from previous page

i have a question. my english isn't well. so i hope i explain well...
i have two tables, tbl_home and tbl_office, the question is
how do i make a select statement from 2 tables which have identical value from column 'case_no' where it is referenced in both table..
$a=$_POST['home_id']
the code above is where i get the home_id from,
while the statement below is how i try to select both tables based on value in column 'case_no' of both table. but it is based on variable $a which i retrieved from form
<?php
$sql2 = "SELECT * FROM tbl_office WHERE case_no IN (SELECT * FROM tbl_home WHERE home_id = '$
$result2=$conn->query($sql2);
while($row = $result2->fetch_assoc()){
$a=$row['case_no'];
$bc=$row['colour'];
echo " $a <br/> ";
echo " $bc2 <br/>";
?>
is the select statement above correct??
soo, i just want anybody to take a look a this specific statement and how to make it right
$sql2 = "SELECT * FROM tbl_office WHERE case_no IN (SELECT * FROM tbl_home WHERE home_id = '$a'";
You need inner join to use:
" SELECT t_office.home_id,t_office.case_no,t_office.name FROM tbl_office
t_office INNER JOIN tbl_home t_home ON t_office.case_no = t_home.case_no;
where t_office.case_no ='$a'";
u can use "inner join" for example:
"SELECT t.home_id,t.case_no,t.name FROM tbl_office
t INNER JOIN tbl_home h ON h.case_no = h.case_no"
**select tbl_home.name,tbl_office.case_no,tbl_office.color from tbl_office
INNER JOIN tbl_home on tbl_office.case_no = tbl_home.case_no
where tbl_office.case_no ='$a';**
I hope this will be working fine until $a(case_no) value is existed in tbl_home or else it doesn't give any rows

joining two tables and fetching values from them error

i am joining the two tables but is giving me error that mysql_fetch_array() expects parameter 1 to be resource,
<?php
$result=mysql_query("SELECT * FROM `photo_gallery`.`photographs` WHERE id=1");
$result .=mysql_query("SELECT * FROM `photo_gallery`.`users` WHERE id=1");
while($row=mysql_fetch_array($result))
{
echo 'You are Welcome'.'<br/>';
$Id=$row['id'];
$Name=$row['username'];
$Batch=$row['password'];
$Address=$row['first_name'];
$Course=$row['last_name'];
$filename=$row['filename'];
$type=$row['type'];
echo 'your ID is'.$Id.'<br/>'.'username '. $Name.'<br/>'.'your password '. $Batch.'<br/>'.'yor first name'. $Address. '<br/>'.'last'.$Course.'<br/>'.'file name is'.'<br/>'.$filename.'<br/>'.'type is '.$type;
}
?>
Here is the most easy syntax to use join function
$query=mysql_query("SELECT * FROM `databasename`.`firstablename` JOIN `seconddatabasename` ON firsttablename.id = secondtablename.id ");
If you want to work with join array than visit this link http://www.w3schools.com/php/func_string_join.asp.
I hope you get what join function is used for.
Try this.
$query = "SELECT * FROM photographs INNER JOIN users ON photographs.id = users.id";
$result = mysql_query($query);
you cannot chain mysql queries in php that way. you have 2 options.
create a real mysql join.
you can use the shorthand syntax:
SELECT * FROM `photographs` p, `users` u WHERE p.id = u.id AND id=1
or a real join:
SELECT * FROM `photographs` p INNER JOIN `users` u WHERE p.id = u.id AND id=1
might i suggest reading more about mysql joins:
http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html

Mysql Join two Id's to get there Usernames

Im trying to get in Array that contains the results from a MYSQL query.
I have 2 ids stored in the table hitlist user_id and mark_id
they need to join in the table users to retrieve there usernames that match there id's and in the future other variables.
i have this working in a weird way and was hopeing to get this working in a more efficent simple way similar to this
$Hitlists = $db->query("SELECT * FROM hitlist JOIN users ON hitlist.user_id = users.id AND hitlist.mark_id = users.id")->fetchAll();
This is the code i have that is working...for now it looks like it might give me problems later on.
<?php
$index = 0;
$Hitlists = array();
$st = $db->query("SELECT * FROM hitlist JOIN users ON hitlist.user_id = users.id")->fetchAll();
$sth = $db->query("SELECT * FROM hitlist JOIN users ON hitlist.mark_id = users.id")->fetchAll();
foreach($st as $id)
{
$Hitlists[] = $id;
}
foreach($sth as $id)
{
$Hitlists[$index]['markedby'] = $id['username'];
$Hitlists[$index]['mark_id'] = $id['mark_id'];
$index++;
}
The way you are joining the table is wrong. You can get the exact records you want, you need to join users table twice to get the username of each ID
SELECT a.*,
b.username User_name,
c.username mark_name
FROM hitlist a
INNER JOIN users b
ON a.user_id = b.id
INNER JOIN users c
ON a.mark_id = c.id
and you can access
$result['User_name']
$result['mark_name']

Extracting member's first name from member table using results from another table

<?php
include 'dbFunctions.php';
$courseid = $_GET['Course_id'];
$query = "SELECT * FROM course WHERE Course_id=".$courseid."";
$arrCourse = executeSelectQuery($query);
$query2 = "SELECT * FROM course_member WHERE Course_id=".$courseid."";
$result = mysqli_query($link,$query2) or die(mysqli_error($link));
?>
HTML body:
<?php
while ($row = mysqli_fetch_array($result)) {
?>
<?php echo $row['member_id']
?>
<?php
}
?>
this correctly prints the results of the member id that is involved in the selected course, however, i want to extract the first_name using the result generated that belong to another table called member.
been trying all the join queries but it didn't work.
Sounds like you need to read up on the JOIN operation in SQL. The following query will return all the data you seem to need, from a single query:
SELECT *
FROM course
INNER JOIN course_member ON course.id = course_member.course_id
INNER JOIN member ON member.id = course_member.member_id
As far as I see
$sql="select last_name from course_member
join member on course_member.member_id = member.member_id
where course_member.Course_id = ".$courseid;
I'm going to take a guess here at your table defs...
$query2 = "SELECT m.first_name, cm.* FROM course_member cm join member m on m.id = cm.member_id WHERE Course_id=".$courseid."";
If you want only the member name and you are not much interested with the id then you can merge it into a single query
$query = SELECT fName from member WHERE id = (SELECT member_id FROM course_member WHERE Course_id=$courseid);

One query instead of two?

Here I'm making two queries with PHP. Is there something more simple? One query instead of two?
$id = mysql_real_escape_string($_GET["id"]);
$result = mysql_query("SELECT * FROM questionstable WHERE id=$id");
$row = mysql_fetch_assoc($result);
$category = $row['category'];
$main = mysql_query("SELECT name FROM categorytable WHERE id=$category");
SELECT questionstable.*, categorytable.name
FROM questionstable
INNER JOIN categorytable
ON categorytable.id = questionstable.category
WHERE questionstable.id=$id
As an aside, assuming your questionstable.id is numeric, you could use $id = (int)$_GET["id"] and save some writing. (It's also probably a safer bet. Just because it's escaped doesn't mean it's completely safe--especially when it's not within quotes [gives you a LOT of options for SQL injection]. ;-))
Please try:
SELECT name
FROM categorytable
WHERE id = (
SELECT category
FROM questionstable
WHERE id = $id
)
$id = mysql_real_escape_string($_GET["id"]);
$main = mysql_query("SELECT c.name FROM categorytable c inner join questionstable q on c.category = q.category WHERE q.id = $id");
Do not use inner join use left join instead, it won't return any result if the category is not found
SELECT questionstable.*, categorytable.name
FROM questionstable
LEFT JOIN categorytable
ON categorytable.id = questionstable.category
WHERE id=$id

Categories