SELECT用于将数据库与相同的表格相乘

时间:2017-08-04 19:00:26

标签: php html mysql mysqli

我试图将两个数据库连接到一个mysql查询中,它也很棒!

之后,我尝试在正确的数据库中查找订单数据,以便从中获取meta_value。

但它混合了meta_value $ first name,有时会在行上复制它们。

如果我只是连接1个数据库,它们会出现,因为任何人都可以看到出现了什么问题?

<?php
$servername = "xxx";
$username   = "xxx";
$password   = "xxx";


$v1  = "_wp282";
$v2  = "_wp111";
$v3  = "_wp72";
$v4  = "_wp193";
$v5  = "_wp555";
$v6  = "_wp366";
$v7  = "_wp74";
$v8  = "_wp721";
$v9  = "_wp924";
$v10 = "_wp253";


// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";

//Here I connect to both databases
$query103 = mysqli_query($conn, "SELECT * FROM  
              `$v1`.`wpd2_posts`  
              where post_status='wc-processing' or post_status='wc-completed' 
                  or post_status='wc-failed' UNION 
       SELECT * FROM `$v2`.`wpd2_posts`  
        where post_status='wc-processing' or post_status='wc-completed' 
          or post_status='wc-failed' order by post_date DESC ") or die(mysqli_error($conn));

?> 

<br> 
      <h2>Database 3</h2> 

        <table style="width: 100%"> 
          <tr> 
           <td>ID</td> 
           <td>??</td> 
           <td>??</td> 
           <td>??</td> 
           <td>??</td> 
           <td>??</td> 


          </tr> 
      <?php
while ($row = mysqli_fetch_array($query103)) {


    // Here I try to find what database the customer is on
    $id2 = $row['ID'];

    if ($row['ID'] == '') {

        echo "intet id";
    } else {

        $query = mysqli_query($conn, "SELECT * FROM  `$v1`.`wpd2_posts` where ID = '$id2' ORDER BY id") or die(mysqli_error($conn));

        if (mysqli_num_rows($query) == '') {

            $query = mysqli_query($conn, "SELECT * FROM  `$v2`.`wpd2_posts` where ID = '$id2' ORDER BY id") or die(mysqli_error($conn));

            if (mysqli_num_rows($query) == '') {

            } else {
                $version = "coalsmil_wp111";

            }

        } else {
            $version = "coalsmil_wp282";

        }


    }

    echo "<tr>";
    echo "<td>" . $row['ID'] . "</td>";
    echo "<td>" . $row['post_date'] . "</td>";
    echo "<td>" . $row['post_status'] . "</td>";

    //And here I try to get the data out


    $querynavn = mysqli_query($conn, "SELECT meta_value FROM  `$version`.`wpd2_postmeta` where meta_key='_shipping_first_name' and post_id='$id2' ") or die(mysqli_error($conn));
    while ($row2 = mysqli_fetch_array($querynavn)) {
        $fornavn = urldecode($row2['meta_value']);
    }
    echo "<td>" . $fornavn . "</td>";
    echo "<td>" . $version . "</td>";
    echo "<td>6</td>";
    echo "</tr>";

}


?>
</table>

1 个答案:

答案 0 :(得分:0)

请勿选择*,但请指定每个列并使用AS命名。像

SELECT `ID` as v1_ID, `post_status` as v1_post_status, `field1` as v1_field1 FROM 
          `$v1`.`wpd2_posts` 
          where post_status='wc-processing' or post_status='wc-completed'
              or post_status='wc-failed' UNION
   SELECT ID` as v2_ID, `post_status` as v2_post_status, `field1` as v2_field1 FROM `$v2`.`wpd2_posts` 
    where post_status='wc-processing' or post_status='wc-completed'
      or post_status='wc-failed' order by post_date DESC

结果数组随后将包含您指定的所有字段名。

另外,请考虑@ tadman对您帖子的评论并确保您的数据库安全。