在PHP

时间:2015-08-24 18:22:35

标签: php mysql arrays multidimensional-array

我需要将位于不同服务器上的两个数据库中的一对MySQL查询合并为一个多维关联数组。以下是我正在使用的数据示例。

这个问题似乎与生成的$ data0数组值有关,缺少唯一键(理想情况下是相应的'sku'值)作为合并数据的索引。

所需的输出是:

Array (
    [0] => Array (
        [SKU] => 101
        [Description] => Test Item 1
        [On Hand 1] => 3
        [On Hand 2] => 7
    )
    [1] => Array (
        [SKU] => 102
        [Description] => Test Item 2
        [On Hand 1] => 11
        [On Hand 2] => 15
    )
)

可能有更好的方法来处理MySQL查询和所需的数组。如果是的话,请告诉我。请提供任何反馈,因为我是初学者,并将获得我能得到的所有帮助。谢谢!

<?
$query1 = "SELECT sku, description, on_hand as 'On Hand 1' FROM database1"

$queryresult1 = mysql_query($query1, $connection1);

$data1 = array();
while ($resultrow1 = mysql_fetch_assoc($queryresult1)) {
    array_push($data1, $resultrow1);
}

$query2 = "SELECT sku, on_hand as 'On Hand 2' FROM database2"

$queryresult2 = mysql_query($query2, $connection2);

$data2 = array();
while ($resultrow2 = mysql_fetch_assoc($queryresult2)) {
    array_push($data2, $resultrow2);
}

$data0 = array_merge($data1, $data2);

//example code for troubleshooting
$data1 = array('101' => array('SKU' => '101', 'Description' => 'Test Item 1', 'On Hand 1' => 3), '102' => array('SKU' => '102', 'Description' => 'Test Item 2', 'On Hand 1' => 11));
print_r($data1);
echo "<br /><br />";
$data2 = array('101' => array('SKU' => '101', 'On Hand 2' => 7), '102' => array('SKU' => '102', 'On Hand 2' => 15));
print_r($data2);
echo "<br /><br />";
$data0 = array_merge($data1, $data2);

print_r($data0);
?>

4 个答案:

答案 0 :(得分:0)

您可能最好使用单个UNION查询:

SELECT sku, description, on_hand as 'On Hand 1' FROM database1
UNION ALL
SELECT sku, null       , on_hand as 'On Hand 2' FROM database2

请注意null用于database2查询中的说明字段。 UNION成员查询必须具有相同数量的列和相同的类型。

然后这是一个简单的问题:

while($row = mysql_fetch_assoc($result)) { 
    $data[] = $row;
}

不需要两个查询,两个获取循环和稍后的合并。

答案 1 :(得分:0)

您可以使用JOIN语法加入查询中的两个表来执行此操作:https://dev.mysql.com/doc/refman/5.0/en/join.html

那么您的查询将如下所示:

SELECT
    table1.sku,
    table1.description,
    table1.on_hand as 'On Hand 1',
    table2.on_hand as 'On Hand 2'
FROM
    database1.table1 JOIN
    database2.table2 ON table1.sku = table2.sku

两个表中的行将彼此连接,其中sku字段值彼此对应。如果你想要table1和table2中的所有结果,你可以创建一个LEFT JOIN

答案 2 :(得分:0)

<?php
// connection 1
$connection1= mysql_connect("localhost","root","");
//selecting database 1
mysql_select_db("database1",$connection1);
// select query from the database1 and table 1
$query1 = "SELECT sku, description, on_hand as 'On Hand 1' FROM database1.table1";
$queryresult1 = mysql_query($query1, $connection1);

$data1 = array();
while ($resultrow1 = mysql_fetch_assoc($queryresult1)) {
    array_push($data1, $resultrow1);
}
//connection 1
$connection2= mysql_connect("localhost","root","");
//selecting database 2
mysql_select_db("database2",$connection2);
// select query from the database2 and table 2
$query2 = "SELECT sku, on_hand as 'On Hand 2' FROM database2.table2";

$queryresult2 = mysql_query($query2, $connection2);

$data2 = array();
while ($resultrow2 = mysql_fetch_assoc($queryresult2)) {
    array_push($data2, $resultrow2);
}

$data0 = array_merge($data1, $data2);

print_r($data0);
?>

答案 3 :(得分:0)

通过循环$data1中的每个键,您可以使用当前$data2$key中检索该值。这将为您提供所需的结果:

$data1 = array('101' => array('SKU' => '101', 'Description' => 'Test Item 1', 'On Hand 1' => 3), '102' => array('SKU' => '102', 'Description' => 'Test Item 2', 'On Hand 1' => 11));
$data2 = array('101' => array('SKU' => '101', 'On Hand 2' => 7), '102' => array('SKU' => '102', 'On Hand 2' => 15));

$data0 = array();
foreach($data1 as $key => $data){
    $data['On Hand 2'] = $data2[$key]['On Hand 2'];
    $data0[] = $data;
}
unset($data1, $data2);

print_r($data0);