I want to distinct a specific column from a table - php

Hi i want to get distinct column data from a table.
my code is below.
how can i make the season column distinct?
<div class="episodes">
<?php
$name=$_GET['name'];
$select=mysql_query("select * from data where name='$name'");
while($show=mysql_fetch_array($select))
{ ?>
<div class="episodes_data">
<div class="t_s_pic">
<img src="admin/photo/movieForsaken-2016-movie-Justin-Price-6-355x500.jpg" height="100%" width="100%"/>
</div>
<div class="t_s_name"><h3>Game Of Thrones<br/>season<?php echo $show['season'];?></h3></div>
<div class="t_s_download">Download series</div>
</div>
<?php } ?>
</div><br>

for obtain distinct column you don't should use select * by explicitally select the column you need eg:
select distinct column1, column2, column3 from data where name='$name'

Related

PHP MYSQL how to show and list all records from two tables using current table's record id?

I have two tables "naujienos" and "apzvalgos" in my SQL Database. I managed to join them both and get all records from both tables using UNION and ORDER BY timestamp. What i need to do is to match records id by current id to show particular record on webpage.
Here is my code :
<?php
$sql="Select * from `naujienos` UNION Select * from `apzvalgos` order by `timestamp` DESC";
$result=mysqli_query($con,$sql);
if($result){
while($row=mysqli_fetch_assoc($result)){
$id=$row['topic_id'];
$topic_image=$row['topic_image'];
$topic_name=$row['topic_name'];
$topic_desc=$row['topic_desc'];
$timeStamp=$row['timeStamp'];
$kategorija=$row['kategorija'];
echo '<div class="card-group pl-5 col-md-3 col-sm-6 mb-5 pb-5">
<div class="card">
<img class="card-img-top paveiksliukas" src='. $topic_image.' alt="Card image cap">
<div class="card-body">
<h5 class="card-title mb-5">'. $topic_name.'</h4>
<p class="card-text">'.substr($topic_desc,0,200).'</p>
Skaityti toliau
</div>
<div class="text-center">
<b>Kategorija: '. $kategorija. '</b>
</div>
<div class="text-center pt-1 pb-1">
<b>Įkelta: '. $timeStamp. '</b>
</div>
</div>
</div>';
}
}
?>
Here is how it looks on webpage (its basically all records from two tables and whenever i click on button i go to that particular record):
This is the code line where when i click on button i get to that particular record by id :
Skaityti toliau
My problem is that when i click on "apzvalgos" table button it sends me to "naujienos" table record, because ive written this:
Skaityti toliau
Right now its only working with "naujienos" records and when i click on "apzvalgos" record it shows me "naujienos" record instead.
Both tables have identical columns.
How can i change the code that when i click on records from "naujienos" and "apzvalgos" table's it shows current record from those two tables?
I've tried to change the code line
Skaityti toliau
to somehow get current table record's id, but i have no idea how to do it.
Can someone help me to fix this problem?
Skaityti toliau
//on naujienos.php file
$id1 = REQUEST['naujienos_id'];
//Show record
$sql = "SELECT * FROM naujienos INNER JOIN apzvalgos ON naujienos.naujienos_id=apzvalgos.apzvalgos_id WHERE naujienos_id='$id1'";
//your code here

How to Show Latest Uploded Post first Using SQL and PHP?

I have a website where I show the post update on the database and we use PHP and SQL for that but currently oldest posts are showing first instead I want to show the latest uploaded Post first.
Here is My PHP code with SQL Query
$projectcat_query=mysql_query("select * from projectcat where id=3 ");
while($projectcat_data=mysql_fetch_assoc($projectcat_query))
{ $catid=$projectcat_data['id'];
$limit=3;
$project_query=mysql_query("select * from projects where catid=$catid and status=1 limit $limit ");
while($project_data=mysql_fetch_assoc($project_query)) { ?>
<div class="item <?php echo $projectcat_data['name']; ?>">
<div class="picframe">
<a class="" href="project/<?php echo str_replace(' ','-',$project_data['title']); ?>">
<span class="overlay">
<span class="pf_text">
<span class="project-name"> <?php echo $project_data['title']; ?></span>
</span>
</span>
</a>
<img src="images/services/<?php echo $project_data['image']; ?> ">
</div>
</div>
<?php } ?>
and here my DB table looks like
Change your 2nd query like this
$project_query=mysql_query("select * from projects where catid=$catid and status=1 order by id desc limit $limit ");
Just add ORDER BY statement to your query like this:
$projectcat_query=mysql_query("select * from projectcat where id=3 ORDER BY id DESC");

how to pick a category name if i chose a news of that category

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'";

Show 5 random item and 5 item by brand mysql php

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"

select all rows with sport=padel

I want to select all the content of the row when sport= padel.
My code is this:
$result = mysqli_query($con,"SELECT sport FROM posts WHERE sport='padel'
");
while($row=mysqli_fetch_assoc($result)){ ?>
<div class= 'box1'>
<p align='middle'><?php echo $row['username']; ?></p>
<h3 align='middle'>Playing</h3>
<h3 align='middle' style= 'color:blue'><?php echo $row['sport']; ?> </h3>
<div><p align='middle' >Disponibilidade:</p>
<h3 align='middle' style='color:blue'><?php echo $row['text']; ?></h3></div>
<p></p>
<h3 align='middle' style='color:blue'><?php echo $row['city']; ?></h3>
<button text-align='middle' id='submit1' type='submit1'>
contact </button>
</div>
This, in fact, select all the row with sport=padel, but all the other content in the different columns is missed (gives me undefined variable)
I think I could do this:
$result = mysqli_query($con,"SELECT sport FROM posts WHERE sport='padel'
WHERE text='' WHERE city=''");
but didn't work. Can anyone help me?
As others have mentioned in the comments, you need to specify what columns you want to pull back, at the moment you are pulling back the sport column only.
SELECT * FROM posts WHERE sport="padel"
This will ensure you pull back all columns. Alternatively you can specify multiple comma separated columns
SELECT col1, col2, col3 FROM posts WHERE sport="padel"

Categories