How to select and echo MySQL data (one to many) using PHP

John Doo

This is my current code, it works fine with one problem.

$stmt = $conn->prepare("SELECT * FROM tmdb_movies JOIN genres ON genres.genres_tmdb_id=tmdb_movies.tmdb_id 
  ");
     // Then fire it up
     $stmt->execute();
     // Pick up the result as an array
     $result = $stmt->fetchAll();

    // Now you run through this array in many ways, for example
     foreach ($result as $row) {
    print "".$row["movie_title"]." ".$row["genres_name"] ."   ".$row["cast_name"] ."<br/>";
}

The thing is, I am trying to echo Movie Name and it's Genres.

What I want to echo:

The Dark Knight - Action, Crime, Drama

What the code echo:

The Dark Knight - Action

The Dark Knight - Crime

The Dark Knight - Drama

Screenshot of main movie table http://prntscr.com/fokwip

and Screenshot of genres movie table http://prntscr.com/fokwoy

Bdloul

To echo what you want you can change your sql query :

$stmt = $conn->prepare("SELECT *, GROUP_CONCAT(genres.genres_name SEPARATOR ', ') AS genre_concat FROM tmdb_movies JOIN genres ON genres.genres_tmdb_id=tmdb_movies.tmdb_id GROUP BY tmdb_movies.movie_title");

//  sql strict group by

$stmt = $conn->prepare("SELECT tmdb_movies.movie_title, tmdb_movies.cast_name, GROUP_CONCAT(DISTINCT genres.genres_name SEPARATOR ', ') AS genre_concat FROM tmdb_movies JOIN genres ON genres.genres_tmdb_id=tmdb_movies.tmdb_id GROUP BY tmdb_movies.movie_title, tmdb_movies.cast_name ORDER BY tmdb_movies.movie_title");

$stmt->execute();

$result = $stmt->fetchAll();

foreach ($result as $row) {
        print "".$row["movie_title"]." ".$row["genre_concat"] ."   ".$row["cast_name"] ."<br/>";}

GROUP_CONCAT allows to concat all the different genre for each unique movie title.

More Documentation :

GROUP BY : https://www.w3schools.com/sql/sql_groupby.asp

GROUP_CONCAT : https://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_group-concat

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to retrieve data from a one to many sql relationship using PHP

From Dev

Select and echo single field from MySQL using PHP

From Dev

PHP select one data from mysql NOT DISPLAYING

From Dev

PHP / MySQL: How to reduce code to echo Select results (working code)

From Dev

Echo data from two different mysql tables using php

From Dev

Trying to display in a simple php system mysql data with One to many relationship?

From Dev

MySQL and PHP One to Many Return Data in Single Row, Multiple Columns

From Dev

How to select users without specific one to many rows in MySQL

From Dev

how to make MYSQL select query for one to many for this case?

From Dev

SELECT and then INSERT data into MySQL using PHP

From Dev

Merging data into one column mysql using php

From Dev

How make a table with php using the echo command with looping data?

From Dev

How make a table with php using the echo command with looping data?

From Dev

Echo Result of MySQL SELECT Query in PHP

From Dev

Basic php - echo mysql, how?

From Dev

to retrieve a mysql data in php and echo the retrieved data

From Dev

How to select information from more then one table in MySQL Database using PHP and mySQli, has to be combined single query

From Dev

mysql select query only showing one data in php

From Dev

how to select data from mysql using timestamp

From Dev

How to select data based on months using mysql

From Dev

Using Ember Data's One to Many or Many to Many, how do you specify the relationship

From Dev

PHP MySQL query 2 tables for data and combine into one json array for echo?

From Dev

How can i make one to many relation in parse using PHP

From Dev

Using only one dataprovider , how to pass data to many test methods

From Dev

How to manage a one to many entity using JPA and Spring Data Rest

From Dev

How to manage a one to many entity using JPA and Spring Data Rest

From Dev

How to get data for one to many relationship using Eloquent in Laravel?

From Dev

How can I echo a date so many days from now using PHP?

From Dev

MySQL - one to many select from two tables

Related Related

  1. 1

    How to retrieve data from a one to many sql relationship using PHP

  2. 2

    Select and echo single field from MySQL using PHP

  3. 3

    PHP select one data from mysql NOT DISPLAYING

  4. 4

    PHP / MySQL: How to reduce code to echo Select results (working code)

  5. 5

    Echo data from two different mysql tables using php

  6. 6

    Trying to display in a simple php system mysql data with One to many relationship?

  7. 7

    MySQL and PHP One to Many Return Data in Single Row, Multiple Columns

  8. 8

    How to select users without specific one to many rows in MySQL

  9. 9

    how to make MYSQL select query for one to many for this case?

  10. 10

    SELECT and then INSERT data into MySQL using PHP

  11. 11

    Merging data into one column mysql using php

  12. 12

    How make a table with php using the echo command with looping data?

  13. 13

    How make a table with php using the echo command with looping data?

  14. 14

    Echo Result of MySQL SELECT Query in PHP

  15. 15

    Basic php - echo mysql, how?

  16. 16

    to retrieve a mysql data in php and echo the retrieved data

  17. 17

    How to select information from more then one table in MySQL Database using PHP and mySQli, has to be combined single query

  18. 18

    mysql select query only showing one data in php

  19. 19

    how to select data from mysql using timestamp

  20. 20

    How to select data based on months using mysql

  21. 21

    Using Ember Data's One to Many or Many to Many, how do you specify the relationship

  22. 22

    PHP MySQL query 2 tables for data and combine into one json array for echo?

  23. 23

    How can i make one to many relation in parse using PHP

  24. 24

    Using only one dataprovider , how to pass data to many test methods

  25. 25

    How to manage a one to many entity using JPA and Spring Data Rest

  26. 26

    How to manage a one to many entity using JPA and Spring Data Rest

  27. 27

    How to get data for one to many relationship using Eloquent in Laravel?

  28. 28

    How can I echo a date so many days from now using PHP?

  29. 29

    MySQL - one to many select from two tables

HotTag

Archive