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.
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'";
Hello please someone can help me with this code? I think there i can remove * and GET id.
<div class="col-md-4">
<h1>Sidebar</h1>
<?php
require_once 'connessione.php';
$id=$_GET['id'];
$query = "SELECT * FROM campi_name where id='$id'";
$stmt = $DBcon->prepare( $query );
$stmt->execute();
while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
?>
<div class="media">
<div class="media-left">
<a href="#">
<img class="media-object" src="..." alt="...">
</a>
</div>
</div>
<?php
}
?>
</div>
How can i show only item (from db) related to the Brand row from table campi_name?
and with code like that how i can show 5 random item from campi_name?
Thank you!
For the first question you can use
$query = "SELECT Brand FROM campi_name WHERE id='" . $id . "'";
I'm not sure because I haven't understand what you are asking. Do you want to select only Brand column or what? Do you want to select just the row that content some letters?
For the second question use
$query = "SELECT * FROM campi_name ORDER BY rand() LIMIT 5"
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";
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