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

标签 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>

最佳答案

您可以毫无问题地在同一查询中访问该表,例如(假设两个数据库中的表具有相同的结构):

 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()");

关于php - 合并并显示2个数据库和2个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48936455/

相关文章:

php - 表单数据库设计

c# - EF :6 - How to skip insertion of value if value has default value set in DB?

mysql - 如何在 MySQL 中允许整数为空字符串?

php - 发布构建 PHP/MySQL 连接查询

php - 如何用 Twig 渲染字符串/数据库中的内容?

javascript - PHP 文件的 jQuery AJAX url 不起作用

php问题eval/html/php

Eclipse 中或外部的 PHPDoc 文档生成器?

mysql - SQL Num Rows 参数/ bool 错误

PHP MySQL 循环/Foreach