[英]How I can cross my two different databases in two different server in PHP?
我执行查询,该查询为我返回HTML表中第一个数据库的字段以及该字段中的一个字段“数字”,并且我想使用相同的字段“编号”查询另一个数据库,以在我的第二个数据库中返回字段“价格” 。
“数字”与我在两个数据库中归档的相同。
如何在php中做到这一点?
这是我的代码PHP:
<?php
if(isset($_POST['date']))
{
$pro_date = $_POST['date'];
$sql = "
SELECT
left(list_product_order.number,8) as number,
list_product_order.created_at as date,
date(list_product_order.received) as received_date,
date(list_product_order.deliv) as deliv_date,
list_product_order.four_name as name,
COUNT(id_product_order) as items,
MONTH(list_product_order.created_at) as month,
-1 as price
FROM `list_product_order`
WHERE YEAR(list_product_order.created_at) = $date
GROUP BY list_product_order.number,month
ORDER BY list_product_order.created_at DESC
";
$datao->exec ( "set names utf8" );
$req = $datao->prepare($sql);
$req->execute();
$fquery = $req -> fetchAll(PDO::FETCH_ASSOC);
}
$sqll = "
SELECT
data_command.number as number,
data_command.sup_name as sup_name,
SUM(data_command_item.value * data_command_item.quantity) as price
FROM data_command
LEFT JOIN data_command_item on data_command.id_number = data_command_item.fk_number
WHERE (data_command_item IN (implode(',',$fquery)))
";
$datam->exec ( "set names utf8" );
$req = $datam->prepare($sqll);
$req->execute();
$squery = $req -> fetchAll(PDO::FETCH_ASSOC);
?>
<div class="panel-body">
<?php if(isset($_POST['pro_date']))
{
foreach($fquery as $key => $value)
{
foreach($squery as $value2)
{
if($value['number'] === $value2['number'])
{
$fquery[$key]['four_name'] = $value2['sup_name'];
$fquery[$key]['price'] = $value2['price'];
}
}
}
echo '
<table class="table table-striped table-bordered table-hover display" id="table_with_sorting" style="zoom: 85%">
<thead>
<tr>
<th style="font-size:11px">Number</th>
<th style="font-size:11px">Date</th>
<th style="font-size:11px">Name</th>
<th style="font-size:11px">Items</th>
<th style="font-size:11px">Received_date</th>
<th style="font-size:11px">Deliv_date</th>
<th style="font-size:11px">Month</th>
<th style="font-size:11px">Price</th>
</tr>
</thead>
<tbody>';
<?php
foreach($fquery as $f)
{
echo "
<tr style='text-align: center;'>
<td>".$f['number']."</td>
<td>".$f['date']."</td>
<td>".$f['name']."</td>
<td>".$f['items']."</td>
<td>".$f['received_date']."</td>
<td>".$f['deliv_date']."</td>
<td>".$f['month']."</td>
<td>".$f['price']."</td>
</tr>";
}
echo" </tbody>
</table>";
?>
<?php }
?>
</div>
</div>
</div>
<?php
?>
</div>
这是我查询的第二个数据库:
SELECT
data_command.number as number,
data_command.sup_name as sup_name,
SUM(data_command_item.value * data_command_item.quantity) as price
FROM data_command
LEFT JOIN data_command_item on data_command.id_number = data_command_item.fk_number
它为我显示一个价格为-1的数字。
谢谢你的帮助。
您可以通过使用多个数据库实例来完成此任务,并将该数据库中的数据加载到单独的数组中(请参见下面的$ arrayOne和$ arrayTwo )。
您的结果集将具有一个关系(请参见下面的“数字”字段),可以使用嵌套的foreach循环将$ arrayTwo值附加到$ arrayOne 。
然后,您可以遍历$ arrayOne并将其显示在html表中。
NB!
下面的代码: (isset($ items ['sup_name'])?$ items ['sup_name']:'无供应商')
检查供应商是否存在,如果不存在,将显示“ No Supplier”。
PHP代码
<?php
//ArrayOne is the result from DB 1
$arrayOne = array(
array("number" => 5, "date" => "12-01-2011", "name"=> "Jack","items" => 30),
array("number" => 8, "date" => "12-05-2015", "name"=> "John","items" => 20),
array("number" => 3, "date" => "12-08-2014", "name"=> "Sarah","items" => 320),
);
//Result from DB 2
$arrayTwo = array(
array("number" => 3, "sup_name" => "Coke","price" =>"25000"),
array("number" => 8, "sup_name" => "Simba","price" =>"1200"),
);
foreach($arrayOne as $key => $value){
foreach($arrayTwo as $value2){
if($value['number'] === $value2['number']){
//add "sup_name" and "price" to arrayOne
$arrayOne[$key]['sup_name'] = $value2['sup_name'];
$arrayOne[$key]['price'] = $value2['price'];
}
}
}
echo '
<table class="table table-striped table-bordered table-hover display" id="table_with_sorting" style="zoom: 85%">
<thead>
<tr>
<th style="font-size:11px">Number</th>
<th style="font-size:11px">Date</th>
<th style="font-size:11px">Name</th>
<th style="font-size:11px">Items</th>
<th style="font-size:11px">Supplier Name</th>
<th style="font-size:11px">Price</th>
</tr>
</thead>
<tbody> ';
foreach($arrayOne as $items)
{
echo "<tr style='text-align: center;'><td>".$items['number']."</td>
<td>".$items['date']."</td>
<td>".$items['name']."</td>
<td>".$items['items']." </td>
<td>". (isset($items['sup_name']) ? $items['sup_name'] : 'No Supplier')." </td>
<td>". (isset($items['price']) ? $items['price'] : '0.00')." </td></tr>";
}
echo '</tbody>
</table> ';
?>
结果
如果我理解的正确,第一个查询的结果将返回一个名为“ number”的列。
现在,您要使用从第一个查询返回的数字来查询另一个数据库。 对?
当您使用同一供应商的数据库时,通常可以跨数据库联接。 但是您也可以“手动”完成。
只需将第一个查询的“数字”存储在一个数组中,然后使用该数组查询第二个数据库即可,例如:
$ArrMyNumbers = array();
// fill it with numbers from first resultset.
// pseudocode, I don't know your exact situation
foreach ($myFirstResultSet as $one){
$ArrMyNumbers[] = (int)$one["number"];
}
And then for your second query:
$SQL2 = "SELECT data_command.number as number,
data_command.sup_name as sup_name,
SUM(data_command_item.value * data_command_item.quantity) as price
FROM data_command
WHERE (data_command_item IN (".implode(",",$ArrMyNumbers).")) "
现在,您可以采用不同的方法来合并第一组数据和第二组数据。
假设您可以用PHP表示结果集,我建议您将整个结果集使用一个键(在这种情况下为“数字”)存储在数组中。 确保它是唯一的! 另外,只需在第一个查询中添加一个名为“ price”的字段,并使其始终为-1,以表示尚未设置。 (这仅适用于您自己,因此您可以轻松地在第二个数据库中找到价格。)
例如:
$sql = "
SELECT
left(list_product_order.number,8) as number,
-1 as price,
list_product_order.created_at as date,
接下来,使用如下代码:
$firstRS = array();
foreach ($data_req as $one){
$firstRS[$one["number"]] = $one;
}
现在,您已映射结果集,其中“数字”为索引。 这样很容易找到正确的行。
现在转到第二个查询,运行它后,您只需在结果集上循环,拾取“数字”和“价格”,然后使用它们来更新第一个结果集(我在这里将其命名为$ firstRS)
就像是:
foreach ($RS_Second as $one){
$firstRS[$one["number"]]["price"] = $one["price"];
}
现在,您可以使用$ firstRS,包括第二个查询的更新价格。
请注意,这是一种适用于不同数据库的通用方法。 当您使用同一数据库供应商时,简单地使用(更复杂的)联接语法ACROSS数据库可能会更容易。 只需在Google上查找您的数据库即可(使用跨数据库X的交叉连接之类的词,其中X是您的数据库)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.