I am an amateur programmer, and I just don't know what's wrong with my query. I want to get stuff out of the database for my landing page, but if I have multiple genres, I get multiple results. I think it has something to do with my query. Bear in mind, I'm not good at programming, nor am I good at SQL I have a table with films called 'films', the primary key is 'id', the table 'genres' has 'genreid' as primary key, and 'filmgenre' (where I inner join) has 'filmgenreid' as primary key. My code:
<?php
if(isset($_GET["id"])){
$filmid = $_GET["id"];
$query = mysqli_query($con, "SELECT *
FROM filmgenre INNER JOIN genres ON
genres.genreid=filmgenre.genreid INNER JOIN
films ON films.id=filmgenre.id WHERE films.id = '$filmid'");
while($film = mysqli_fetch_array($query)){
?>
<img class="filmimage" src="<?php echo $film["plaatje"] ?>" />
<h2 class="filmtitle"><?php echo $film["titel"] ?></h2><br>
<p class="tijdsduur">Tijdsduur: <?php $film["tijdsduur"] ?> minuten.</p>
<iframe width="854" height="480" src="<?php echo $film["trailer"] ?>" frameborder="0" allowfullscreen></iframe>
<p class="beschrijving"> <?php echo $film["voorwoord"] ?></p>
<p class="producent">Producent <?php echo $film["producent"] ?>.</p>
<p class="auteurs">Auteurs: <?php echo $film["auteurs"] ?>.</p>
<p class="jaar"><?php echo $film["jaar"] ?>.</p>
<?php echo $film["hoofdpersonen"] ?>
<?php
}
}
else{
echo "Something went wrong.";
}
?>
Can anyone help?
Use group by,
WHERE films.id = '$filmid'" group by films.id;
Cant really understand what you want exactly but could you give an example of what you should expect plus what you are getting instead
SELECT * FROM filmgenre fg INNER JOIN genres g ON fg.genreid =g.genreid
INNER JOIN films f ON fg.id = f.id WHERE f.id = '$filmid' GROUP BY g.genreid";
Related
I am setting a query where i chose an ID of news to show that specific id news, i have a foreign key of category table as category ID in news table, now i want that if i show a news i show title of category also.
I have tried picking up the category id which show only the id and i want to show the title of that category.
<?php
if(isset($_GET['news_id'])){
$the_news_id = $_GET['news_id'];
}
$query = "SELECT * FROM news_title WHERE news_id = '$the_news_id'";
$select_all_news_query = mysqli_query($connection,$query);
while($row= mysqli_fetch_assoc($select_all_news_query)){
$title = $row['news_title'];
$description = $row['news_description'];
$image = $row['news_image'];
$news_cat_title= $row['news_cat_id'];
?>
<h1 class="page-header">News Page</h1>
<!-- First Blog Post -->
<h2>
<?php echo $title;?>
</h2>
<img class="img-responsive" src="image/<?php echo $image; ?>"
alt="abc">
<hr>
<p><?php echo $description; ?></p>
<hr>
<?php }
?>
<!-- Second Blog Post -->
<hr>
<div class="container">
<div class="row">
<h1 class="page-header">
<!--This is where i want to show title but i am getting the ID--!>
<?php echo $news_cat_title; ?>
</h1>
</div>
</div>
i expect to get the title but i am getting a number.
Without knowing the schemas for the various tables it is hard to say for sure if I interpreted the question correctly but I assume what you are trying to do is join two tables? If you can you add relevant table schemas it would help.
select * from news_title t
left outer join category c on c.category_id=t.category_id
where news_id = '$the_news_id'
Based upon your last comment that the category table is simply called category and the foreign key is cat_id
select * from news_title t
left outer join category c on c.cat_id=t.cat_id
where news_id = '$the_news_id'
Then, when displaying the recordset you should be able to:-
echo $row['cat_title'];
An easy way ( on windows ) to capture and display the table schema:
Open cmd prompt
type mysql -u root -p <DBNAME> ~ changing for the actual database!
type describe <TABLE>; - press return
copy displayed info and paste into text editor
Repeat for all tables of relevance
Please try this code :
<?php
if(isset($_GET['news_id'])){
$the_news_id = $_GET['news_id'];
}
$query = "SELECT N.*, NC.cat_id AS news_cat_id
FROM news_title AS N
LEFT JOIN news_category AS NC
ON N.cat_id = NC.cat_id
WHERE news_id = '$the_news_id'";
$select_all_news_query = mysqli_query($connection,$query);
while($row= mysqli_fetch_assoc($select_all_news_query)){
$title = $row['news_title'];
$description = $row['news_description'];
$image = $row['news_image'];
$news_cat_title= $row['news_cat_id'];
?>
<h1 class="page-header">News Page</h1>
<!-- First Blog Post -->
<h2>
<?php echo $title;?>
</h2>
<img class="img-responsive" src="image/<?php echo $image; ?>" alt="abc">
<hr>
<p><?php echo $description; ?></p>
<hr>
<?php } ?>
<!-- Second Blog Post -->
<hr>
<div class="container">
<div class="row">
<h1 class="page-header">
<!--This is where i want to show title but i am getting the ID--!>
<?php echo $news_cat_title; ?>
</h1>
</div>
</div>
The query statement based on your question and comments is maybe like this :
$query = "SELECT N.*, NC.cat_id AS news_cat_id
FROM news_title AS N
LEFT JOIN news_category AS NC
ON N.cat_id = NC.cat_id
WHERE news_id = '$the_news_id'";
I have a problem and I don't know how to resolve...
I have page with comments and page with replies.
Down is my code, everything is ok, it shows all I want but the problem is, when I click on the comment to go to the page with a reply, the id it gets is wrong.
Example: for the first comment I need to have id=1, second id=2 etc. But I get id=1 for all the comments where user_id = 1 and not id from comment.
Sorry, my English.
<?php
$id = $show['id'];
$sq = "SELECT * FROM comments, users WHERE comments.user_id = users.id";
$re = mysqli_query($dbCon, $sq);
while($abc=mysqli_fetch_assoc($re)){
?>
<div class="card hoverable q_area">
<div class="card-content">
<div class="chip">
<img src="<?php echo $show['profile_foto'] ?>">
<?php echo $abc['user'] . " said:"; ?>
</div><br />
<div id="comm">
<?php $a=substr(str_replace(' ','-',$abc['question']), 0, 50); ?>
<h5><a href="replys.php?id=<?php echo $abc['id'] ?>&reply=<?php echo $a ?>">
<?php echo $abc['comment']; ?></a></h5> <br />
</div>
</div>
</div>
<?php } ?>
If I change that select like this:
$sq = "SELECT * FROM comments";
then link id work, but I can't echo the user who said the comment.
database tables:
users - id, user, pass
comments- id, user_id, comment
please help me :(
Change Your query - $sq to this one:
$sq = "SELECT a.*, b.user FROM comments as a, users as b WHERE a.user_id = b.id"
You just nedd a alias in your select;
$sq = "SELECT *, comments.id as comment_id FROM comments, users WHERE comments.user_id = users.id";
And your link will be some like this:
<h5><a href="replys.php?id=<?php echo $abc['comment_id'] ?>&reply=<?php echo $a ?>">
I hope I've helped.
Hello first of all what i am doing in , i am coding a website for advertise .
Now what do i need is a help to display a lots of data from two tables of database .
What i have done so far u can check at My project you have to login use (Username : test , password : 123456a) to login , so there is everything is okay except an image image are the same on every ads and i do not find the way to make it right .
So i have a "posts" table with an information about ads and an "images" table with a path of an image this is how its looks like :
and this is my code :
<?php
$userid = $_SESSION["userid"];
$sql = "SELECT * FROM posts WHERE userid='$userid' ";
$res = mysqli_query($connect,$sql);
while ($row = mysqli_fetch_assoc($res)) {
?>
<div id="ads">
<div id="titlepic">
<?php echo $row["title"]; ?><br>
<img src="<?php echo $Photo[0]; ?>" height="100px;">
</div>
<div id="managead">
Edit<br style="margin-bottom: 5px;">
Delete<br style="margin-bottom: 5px;">
Renew
</div>
<div id="dates">
<b>Date Added:</b> <?php echo date('m/d/Y', $row["dateadded"]); ?><br>
<b>Renew Date:</b> <?php if($row["renewdate"] > 0){ echo date('m/d/Y', $row["renewdate"]); } ?><br>
<b>Location:</b> <?php echo $row["location"]; ?><br>
<b>Price:</b> <?php echo $row["price"]; ?><br>
</div>
</div>
<hr width="100%">
<?php
so the question is how to extract and images from other table at the same time or how tu run two query at the same time and get an information from them
your SQL statement needs a JOIN in order to include data from two tables in one query.
$sql = "
SELECT *
FROM posts p
JOIN images i
ON p.id = i.postid
WHERE p.userid='$userid'
";
this result set will be populated with all columns from both tables. now you can access path1 via:
<?php echo $row["path1"]; ?>
while this will work for all of your images, such as $row["path2"], $row["path3"], etc, keep in mind this is a bad design for a many-to-many relationship, so it should be normalized to include a linking table which would hold all of your images.
I'm making a PHP SQL db to catalog short stories. On my index page that shows little excerpts of the stories, I'm trying to display the sql rows from a table (stories) as an unordered list using the while looping function. Ratings for the stories are held in another table and I'm linking it to the 'stories' table using LEFT JOIN and the unique column rows "id" and 'storyid'.
Upon loading, the data displays just fine, however only one SQL row is showing, and my limit is set to 50. The while loop script and sql(close) script flank the list item. No error messages are reporting.
can anyone suggest why only 1 item is showing?
<?php
include("db.php");
$query="SELECT s.*, AVG(r.rank) AS avrank
FROM (SELECT *
FROM stories
WHERE id BETWEEN 1 AND 100
ORDER BY RAND()
LIMIT 50) AS s
LEFT JOIN ratings AS r ON r.storyidr = s.id";
$result=mysqli_query($connection,$query);
?>
<ul id="tiles">
<?php
while ($data = mysqli_fetch_assoc($result)):
$id = $data['id'];
$author = $data['author'];
$page_path = $data['page_path'];
$title = $data['title'];
$avgrate = $data['avrank'];
if(is_null($page_path)){$page_path = "#";}
?>
<li>
<div class="post-info">
<h3><?php echo $title; ?></h3>
<h3>rating is <?php echo $avgrate; ?>/5</h3>
<span><a href="categories/<?php echo $category; ?>.php">
<label> </label>
</span> </div>
<div class="post-info-rate-share">
<form method="POST" action="rating.php?id=<?php echo $id; ?>">
<fieldset class="rating">
<legend> Rating: <?php echo $avgrate=round($avgrate,2); ?>/5</legend>
<input type="radio" id="star5" name="starno" value="5" onclick="this.form.submit()"/>
</fieldset>
</form>
</div>
</li>
<?php
endwhile;
mysqli_close($connection);
?>
</ul>
You missed the GROUP BY:
SELECT s.*, AVG(r.rank) AS avrank
FROM stories s
LEFT JOIN ratings AS r ON r.storyidr = s.id
WHERE id BETWEEN 1 AND 100
GROUP BY s.id
ORDER BY RAND()
LIMIT 50;
Demo: http://sqlfiddle.com/#!2/7a9fa/9
I have two queries from two tables one is properties and second is images of properties.
The PropertyID field is available in both tables which have relationship of PK--->FK.
NOW my question is here how can I use the value of propertyID which I have received in
the first query and use it in the second one for retrieving the images of each property.
I 've wrote some code, but I receive this error message:
Notice: Trying to get property of non-object in C:\xampp\htdocs\Scale-Property\spd\index.php on line .......
Here is my code:
<?php
require_once('../Admin Panel/db.php');
if(isset($_POST['Province']) && isset($_POST['District']) && isset($_POST['radio']))
{
$provincename=$_POST['Province'];
$districtname=$_POST['District'];
$propertystatus=$_POST['radio'];
$query = "SELECT
properties.PropertyID,
properties.PropertyName,
some other fields......,
Provinces.ProvinceName,
districts.DistrictName,
pds.PDName,
propertyimages.PropertyID
FROM properties, provinces, districts, pds, propertyimages
WHERE Provinces.ProvinceID=Properties.ProvinceID
AND districts.DistrictID=Properties.DistrictID
AND pds.PDID=properties.PDID
AND ProvinceName='".$provincename."'
AND DistrictName='".$districtname."'
AND PropertyDealType='".$propertystatus."'
ORDER BY properties.PropertyID";
$queryrun= $connection->query($query); // first query run in here
while ($row= $queryrun->fetch_assoc()) // in here trying to store the propretyID
{
if( $connection->error ) exit( $connection->error );
$count= $queryrun->num_rows;
echo 'You Have Got <b>'. $count .' </b>out of 326 Records';
while($row = $queryrun->fetch_assoc())
{
$imagequery ="SELECT PropertyID, ImagePath, ImageName, FROM properties WHERE PropertyID = '".$row['PropertyID']."'";
// Now i want to use the stored value of propertyID in here for retrieving the
Images of related property
}
$imagequery_run= $connection->query($imagequery);
if($imagequery_run->num_rows > 0)
{
while ($imagerow = $imagequery_run ->fetch_assoc())
{
?>
<div class="propertywrapperviewmore">
<div class="propertysingleimageviewmore">
<a href="property.php?PropertyID=<?php
echo htmlentities($imagerow['PropertyID']) ?>&PropertyID=<?php echo htmlentities($propertyrow['PropertyID']) ?>">
<img src="<?php echo htmlentities($imagerow['ImagePath']) ?>" width="227" height="147" alt="<?php echo htmlentities($imagerow['ImageName']) ?>" ></a>
</div>
<div class="propertyIDviewmorelablevalue">
<div class="propertyIDL">Property ID:</div>
<div class="propertyIDV"><?php echo $row['PropertyID']?></div>
</div>
<div class="propertyIDviewmorelablevalue">
<div class="propertyIDL">Property Name:</div>
<div class="propertyIDV"><?php echo $row['PropertyName']?></div>
</div>
</div>
<?php
}
}
}
}
?>
Like Burhan Khalid suggested, you need to join the tables like this:
select [listOfFields]
from properties p
join propertyimages pi
on p.propertyid = pi.propertyid
where [youWhereClauses]
Where the on part links the PK and FK of the tables.
Check the mysql syntax here: http://dev.mysql.com/doc/refman/5.0/en/join.html