PHP pull from two MySQL tables, where multiple rows in table two

Kenneth J

I have two tables that looks like this:

report table

picture table

Put it in images since i dont know how to draw a table here.

My problem is that i can't seem to make a query or anything in my php that will allow me to load the report and the 5 images, so that i can display the images where i want to on the page. As i do it now it loads the report five times and one image in each report.

The edited code after @Terminus suggestions

  $sql = "
  SELECT *
  FROM fangstrapporter AS f, rapportbilleder AS r
  WHERE f.id=".htmlspecialchars($_GET["id"])." AND r.id=f.id";

  $result = $conn->query($sql);
  if ($result->num_rows > 0) {
    $row = $result->fetch_assoc();
    echo "<b>Overskrift:</b> " . $row["overskrift"] . "<br><br>" . 
    "<b>Sted:</b> " . $row["sted"] . "<br><br>" . 
    "<b>Fangstdato:</b> " . $row["fangstdato"] . "<br><br>" . 
    "<b>Agn:</b> " . $row["agn"] . "<br><br>" . 
    "<b>Kategori:</b> " . $row["kategori"] . "<br><br>" . 
    "<b>Art:</b> " . $row["art"] . "<br><br>" . 
    "<b>Vægt:</b> " . $row["vaegt"] . "<br><br>" . 
    "<b>Længde:</b> " . $row["laengde"] . "<br><br>";

    do {  
      echo "<a href='" . $row["image_path"] . "'><img src='" . $row["image_thumb_path"] . "'></a><br><br>";
    } while($row = $result->fetch_assoc());

    echo $row["beskrivelse"]."<br>";
  } else {
    echo "0 results";
  }

Can anyone help me do this? I have been searching on google for four days now, without any success.

user5051310

Do as @Kenney suggested and remove the part where you echo the report from the loop.

$sql = "
  SELECT *
  FROM fangstrapporter AS f, rapportbilleder AS r
  WHERE f.id=".htmlspecialchars($_GET["id"])." AND r.id=f.id";

  $result = $conn->query($sql);
  if ($result->num_rows > 0) {
    $row = $result->fetch_assoc();
    echo "<b>Overskrift:</b> " . $row["overskrift"] . "<br><br>" . 
    "<b>Sted:</b> " . $row["sted"] . "<br><br>" . 
    "<b>Fangstdato:</b> " . $row["fangstdato"] . "<br><br>" . 
    "<b>Agn:</b> " . $row["agn"] . "<br><br>" . 
    "<b>Kategori:</b> " . $row["kategori"] . "<br><br>" . 
    "<b>Art:</b> " . $row["art"] . "<br><br>" . 
    "<b>Vægt:</b> " . $row["vaegt"] . "<br><br>" . 
    "<b>Længde:</b> " . $row["laengde"] . "<br><br>";
     $beskrivelse = $row["beskrivelse"];

    do {  
      echo "<a href='" . $row["image_path"] . "'><img src='" . $row["image_thumb_path"] . "'></a><br><br>";
    } while($row = $result->fetch_assoc());

    echo $beskrivelse . "<br>";
  } else {
    echo "0 results";
  }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

PHP and MySql - fetching rows from two tables

From Dev

Insert multiple rows from two tables into one table

From Dev

Insert rows into a table from another two tables separately in mysql

From Dev

mysql select from multiple tables where two items match

From Dev

mysql select from table where two (or more) rows match,

From Dev

MySql multiple select from two tables and join their results to a third table

From Dev

MySQL JOIN two tables and return multiple rows

From Dev

Compare Two Tables, Get Value From Table and Multiply It - PHP MySQL

From Dev

MySQL update field with WHERE from two tables

From Dev

mySQL multiple select from two tables (filtering)

From Dev

MYSQL Combing Two tables From Multiple Row

From Dev

Need mysql query to pull data from two tables

From Dev

Select rows from a table based on results from two other tables in mySQL using PDO

From Java

sql two where clauses from one table into two new rows

From Dev

In MySQL, how to copy rows of two tables within the same table respectably using PHP?

From Dev

Get a list of all rows from one table, that satisfies multiple criteria in two different tables

From Dev

Json from two mysql tables in PHP

From Dev

PHP MYSQL Newsletter with addresses from two tables

From Dev

php, MySql subquery from two tables

From Dev

Php, MySql Sum from two tables

From Dev

Display data from two tables - PHP mySQL

From Dev

MySQL 'multiple rows' from two joins challenge

From Dev

PHP mysql how to join two tables to link name from one table and post from other table

From Dev

SQL two table LEFT JOINs with multiple WHERE conditions from both tables

From Dev

How to do LEFT JOIN two tables and exclude multiple rows from main query in subquery using MySQL?

From Dev

MYSQL: How to insert rows in a table based on a condition of other two tables

From Dev

Selecting data from two mysql tables where there may be nonexistent data in second table

From Dev

How to select data from columns in only one table when checking two tables limited by where clause in MySQL

From Dev

How to select data from columns in only one table when checking two tables limited by where clause in MySQL

Related Related

  1. 1

    PHP and MySql - fetching rows from two tables

  2. 2

    Insert multiple rows from two tables into one table

  3. 3

    Insert rows into a table from another two tables separately in mysql

  4. 4

    mysql select from multiple tables where two items match

  5. 5

    mysql select from table where two (or more) rows match,

  6. 6

    MySql multiple select from two tables and join their results to a third table

  7. 7

    MySQL JOIN two tables and return multiple rows

  8. 8

    Compare Two Tables, Get Value From Table and Multiply It - PHP MySQL

  9. 9

    MySQL update field with WHERE from two tables

  10. 10

    mySQL multiple select from two tables (filtering)

  11. 11

    MYSQL Combing Two tables From Multiple Row

  12. 12

    Need mysql query to pull data from two tables

  13. 13

    Select rows from a table based on results from two other tables in mySQL using PDO

  14. 14

    sql two where clauses from one table into two new rows

  15. 15

    In MySQL, how to copy rows of two tables within the same table respectably using PHP?

  16. 16

    Get a list of all rows from one table, that satisfies multiple criteria in two different tables

  17. 17

    Json from two mysql tables in PHP

  18. 18

    PHP MYSQL Newsletter with addresses from two tables

  19. 19

    php, MySql subquery from two tables

  20. 20

    Php, MySql Sum from two tables

  21. 21

    Display data from two tables - PHP mySQL

  22. 22

    MySQL 'multiple rows' from two joins challenge

  23. 23

    PHP mysql how to join two tables to link name from one table and post from other table

  24. 24

    SQL two table LEFT JOINs with multiple WHERE conditions from both tables

  25. 25

    How to do LEFT JOIN two tables and exclude multiple rows from main query in subquery using MySQL?

  26. 26

    MYSQL: How to insert rows in a table based on a condition of other two tables

  27. 27

    Selecting data from two mysql tables where there may be nonexistent data in second table

  28. 28

    How to select data from columns in only one table when checking two tables limited by where clause in MySQL

  29. 29

    How to select data from columns in only one table when checking two tables limited by where clause in MySQL

HotTag

Archive