douniang3866 2017-08-03 19:44
浏览 22

SELECT 2相同的数据库,在一个列表中获得2个网站订单

I'm trying to connect two databases into a mysql query and it's also great!

After that, I try to find data on the order in the correct database to get meta_value from it.

But it mixes meta_value $ first name and sometimes duplicates them on the rows.

If I just conect 1 database, they will come out as they should Anybody can see what goes wrong?

Url to example: https:// http://kundeservice.coalsmile.com/test4.php

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


  $v1 = "coalsmil_wp282";
  $v2 = "coalsmil_wp111";
  $v3 = "coalsmil_wp72";
  $v4 = "coalsmil_wp193";
  $v5 = "coalsmil_wp555";
  $v6 = "coalsmil_wp366";
  $v7 = "coalsmil_wp74";
  $v8 = "coalsmil_wp721";
  $v9 = "coalsmil_wp924";
  $v10 = "coalsmil_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条回答 默认 最新

  • dongtang8678 2017-08-03 19:49
    关注

    If you're connections are on the same database server you can specify the schema (database name) to select things from both over one connection. You do this by using schema_name.table_name instead of just the table's name.

    So like:

    SELECT * FROM schema_name.wpd2_posts WHERE ...
    

    In your case you could use UNION to put it all together:

    SELECT * FROM `schema1.wpd2_posts` 
       where post_status='wc-processing' or post_status='wc-completed'
        or post_status='wc-failed' UNION
    SELECT * FROM `schema2.wpd2_posts` 
       where post_status='wc-processing' or post_status='wc-completed'
        or post_status='wc-failed' order by post_date DESC LIMIT 10
    

    On a side note you could make this query a bit more readable by using the IN() clause for your critera:

    WHERE post_status IN('wc-processing', 'wc-completed','wc-failed')
    
    评论

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名