合并并显示2个数据库和2个表

时间:2018-02-22 20:16:58

标签: php mysql

嘿伙计们我有问题,有一个页面,我需要根据日期显示数据,但是来自2个数据库和来自每个数据库的2个表,

1数据库名称= highmob_comenzi表格=玩家和vanzari
2数据库名称= highmob_comenzi2表=玩家和vanzari

这是我得到的代码,我尝试过“选择*来自玩家,vanzari”,但仍然没有运气,即使不会从两个数据库中提取数据:(

<table class='table table-responsive-sm table-bordered table-striped table-    sm' border="2px">
<thead>
<tr>
<th>Locatia Vanzari</th>
<th>Tip. Cert.</th>
<th>Nr.</th>
<th>Status Comanda Mobila</th>
<th>Status Comanda Tapiterii</th>
<th>Status Livrare</th>
<th>Ora Livrare</th>
<th>Detalii Comanda</th>
<th>Total</th>
<th>Avans</th>
<th>Rest</th>
<th></th>
</tr>

<?php
$servername = "localhost";
$username = "id";
$password = "pw";
$dbname = "highmob_comenzi2";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 

$sql = ("select *
 from highmob_comenzi.players 
 cross join highmob_comenzi.vanzari
 union all 
 select *
 from highmob_comenzi2.players 
 cross join highmob_comenzi2.vanzari
  WHERE statuslivrare >= CURRENT_DATE()");
$result = $conn->query($sql);


if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
    echo "


</thead>
<tbody>
<tr>

<td>HERE DATABASE NAME</td>
<td><a href='vezibilettransportcomenzi.php?id=" . $row["id"] . "'     target='_blank'  class='btn btn-sm btn-warning'>Tip. Cert.</a></td>
<td>" . $row["id"] . "</td>
<td>
" . $row["statuscomanda"] . "
</td>
<td>" . $row["statuscomandatapiterii"] . "</td>
<td>" . $row["statuslivrare"] . "</td>
<td>" . $row["oralivrare"] . "</td>
<td>" . $row["detaliicomanda"] . "</td>

<td>
 " . $row["totaldeplata"] . "</td>
<td>" . $row["avans"] . " <br><a style='color:red;'>" . $row["banipreluati"]     . "</a></td>
<td>" . $row["restdeplata"] . "<br><a style='color:red;'>" .     $row["banipreluatirest"] . "</a></td>
<td><a href='edit.php?id=" . $row["id"] . "'  target='_blank' class='btn     btn-sm btn-primary' >Vezi comanda</a></td>
</tr>





    ";
    }
} else {
echo "Nu sunt transporturi!";
}

$conn->close();
?> 
</tbody>
</table>

2 个答案:

答案 0 :(得分:0)

你可以在没有问题的情况下访问同一个查询中的表格(假设表格在数据库中都有相同的结构):

 select *
 from highmob_comenzi.players 
 cross join highmob_comenzi.vanzari
 union all 
 select *
 from highmob_comenzi2.players 
 cross join highmob_comenzi2.vanzari

这只是在单个查询中访问2数据库的示例。您必须重新考虑查询与您的加入和条件

但基于你的评论似乎你只需要(假设statuslivrare是vanzari表的列)

 select *
 from highmob_comenzi.players 
 cross join highmob_comenzi.vanzari
 WHERE vanzari.statuslivrare >= CURDATE()");

答案 1 :(得分:0)

 <?php
 $servername = "localhost";
 $username = "ID";
 $password = "PW";
 $dbname1 = "highmob_comenzi2";


 // Create connection
 $conn = new mysqli($servername, $username, $password, $dbname1 );
 // Check connection
 if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
 } 

 $sql = ("select * from highmob_comenzi2.players cross join      highmob_comenzi2.vanzari
   ");
      $result = $conn->query($sql);


 if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
    echo "


 </thead>
 <tbody>
 <tr>

 <td>HERE DATABASE NAME</td>
 <td><a href='vezibilettransportcomenzi.php?id=" . $row["id"] . "'      target='_blank'  class='btn btn-sm btn-warning'>Tip. Cert.</a></td>
 <td>" . $row["id"] . "</td>
 <td>
 " . $row["statuscomanda"] . "
 </td>
 <td>" . $row["statuscomandatapiterii"] . "</td>
 <td>" . $row["statuslivrare"] . "</td>
 <td>" . $row["oralivrare"] . "</td>
 <td>" . $row["detaliicomanda"] . "</td>

 <td>
  " . $row["totaldeplata"] . "</td>
 <td>" . $row["avans"] . " <br><a style='color:red;'>" .      $row["banipreluati"] . "</a></td>
 <td>" . $row["restdeplata"] . "<br><a style='color:red;'>" .      $row["banipreluatirest"] . "</a></td>
 <td><a href='edit.php?id=" . $row["id"] . "'  target='_blank' class='btn      btn-sm btn-primary' >Vezi comanda</a></td>
 </tr>





        ";
     }
 } else {
     echo "Nu sunt transporturi!";
 }

 $conn->close();
 ?> 

这是我得到的和它的工作,它显示来自1个数据库nammed highmob_comenzi2和表格玩家和vanzari的数据,但我在显示器上得到了很多重复项,我还需要插入一个WHERE子句:

WHERE statuslivrare&gt; = CURRENT_DATE()

任何想法?