Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying values from two different tables inside dropdownlist using php

I have two database tables, released_movies and upcoming_movies. I need to display the field rel_movies in release_movies table and up_movies field in upcoming_movies table inside a single dropdown list. Below is my code.

<select name="film" class="form-control">
<?php
include "connection.php";
$sql="select rel_movies from released_movies";
$sqlr=$con->query($sql);
while($row=$sqlr->fetch_assoc())
{
?>
<option value="<?php echo $row['rel_movies'];?>"><?php echo $row['rel_movies'];?></option>
<?php
}
?>
</select>

I have no idea how to display up_movies field into it. Is this possible .Thanks in advance.

like image 962
Oops Avatar asked Jul 25 '26 14:07

Oops


2 Answers

You may use UNION in mysql for getting records from more than one table.
In your case you may use UNION for getting released as well as upcoming movies, no need of execution of two MySQL query.

<select name="film" class="form-control">
<?php
include "connection.php";
$sql="select rel_movies as movies_name from released_movies 
UNION select up_movies as movies_name from upcoming_movies";
$sqlr=$con->query($sql);
while($row=$sqlr->fetch_assoc())
{
?>
<option value="<?php echo $row['movies_name'];?>"><?php echo $row['movies_name'];?></option>
<?php
}
?>
</select>

More info about UNION: UNION

like image 169
Shrey Prajapati Avatar answered Jul 27 '26 02:07

Shrey Prajapati


Try this

    <?php include "connection.php"; ?>
    <select name="film" class="form-control">
    <?php
        $sqlr="select rel_movies from released_movies";
        $sqlrr=$con->query($sqlr);
        while($row=$sqlrr->fetch_assoc()) { ?>
    <option value="<?php echo $row['rel_movies'];?>">
<?php echo $row['rel_movies'];?>
</option>
    <?php }
        $sqlu="select up_movies from upcoming_movies";
        $sqluu=$con->query($sqlu);
        while($rowu=$sqluu->fetch_assoc()){ ?>
    <option value="<?php echo $rowu['up_movies'];?>">
<?php echo $rowu['up_movies'];?>
</option>
    <?php } ?>
    </select>
like image 45
Ali Zia Avatar answered Jul 27 '26 03:07

Ali Zia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!