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>