L
L
lesmanora2021-01-25 15:42:25
SQL
lesmanora, 2021-01-25 15:42:25

How to combine 2 bases in one query?

There are two tables. One contains books, the other a link to the store with this book.
On the page with a general list of books, a store should also be displayed under each book. Question. How to do it? In this query, all books are displayed below, but stores are not pulled up.

spoiler
<?php
 
$link = mysqli_connect($db_host, $db_user, $db_password, $db_database) 
    or die("Ошибка " . mysqli_error($link)); 
     
     
$query ="SELECT * FROM book  ORDER BY id  ";
 
$result = mysqli_query($link, $query) or die("Ошибка " . mysqli_error($link)); 


if (mysqli_num_rows($result) > 0)
{
 $row = mysqli_fetch_array($result); 
 
 do
 {
  
  $img_pathh = 'uploads_images/'.$row["image"];

  
             echo '	 
    
    
      <div class="row justify-content-center">
          
          <div class="col-md-4 books-content-img">
          	<a href="book.html"><img src="'.$img_pathh.'"/></a>
          </div>
          
          <div class="col-md-5 books-annotation">
          
          
          <div class="books-annotation-content">
          <div class="books-annotation-title">
          	<h2><a href="book.php">'.$row["title"].'</a></h2>
          	
          	<p>Жанр: <span>'.$row["genre"].'</span></p>
          	<p>Возрастное ограничение: <span>'.$row["age"].'</span></p>
          	</div>
          	
          <div class="books-annotation-text">
          	<p>'.$row["miniannotation"].'</p>
          	</div>
          </div>	
          	
          	<div class="books-annotation-read">
          		
          		<h3>Купить и читать:</h3>
          		<ul class="hr">
          		
          		';
          		
          		
          		$query1 ="SELECT * FROM book WHERE id='$id' ";
 
$result1 = mysqli_query($link, $query1) or die("Ошибка " . mysqli_error($link)); 


if (mysqli_num_rows($result1) > 0)
{

 

$query1 ="SELECT * FROM tovar WHERE products_id='$id' ";
 
$result1 = mysqli_query($link, $query1) or die("Ошибка " . mysqli_error($link)); 


if (mysqli_num_rows($result1) > 0)

{
$row1 = mysqli_fetch_array($result1);

do

{

             echo '	 
          		
          			<li><a href="'.$row1["tovar"].'" target="_blank">'.$row1["many"].'</a></li>
          	';
                }	while ($row1 = mysqli_fetch_array($result1));
}	}	
          			
    echo '	 	    			
          			
          		</ul>
          		
          	</div>
          	
          </div>
          	
      </div>
      
    ';	
    
}
    while ($row = mysqli_fetch_array($result));
}							
?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
d-stream, 2021-01-25
@d-stream

So it is necessary to "combine" databases, tables, instances or servers? All this is a little different.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question