0

I looked through the suggested other posts though I didn't see what I was looking for. This may seem basic to most of you here, so please forgive my simplicity, but I'm stuck. So below is what I have and it's working. (not everything is showing of the page). What I want to do is call the "payment_status" from a different database completely. What my question is, is how can I use this, and then have another script within this just to pull that column from a different database. I'm using PHP.

What I have

    <?php
    $servername = " ";
    $username = " ";
    $password = " ";
    $dbname = " ";
    
    
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
    }
    
    $sql = "SELECT id, name, email, payment_status, FROM paymenttable";
    $result = $conn->query($sql);
    
    echo "<div id='firstentry" . $row["id"]. "'><font color='yellow'>Name:</font> <span class='name'>" . $row["name"]. "</span> <br>";   // This uses the first database listed
    
    echo "<font color='yellow'>Payment Status:</font>  <span class='payment_status'>" . $row["payment_status"]. "</span>";  // This is what needs called from a different database than the rest of the page

    echo "<div id='next entry" . $row["id"]. "'><font color='yellow'>Email:</font> <span class='email'>" . $row["email"]. "</span> <br>";  // This uses the first database listed
3
  • 1
    I’m not sure where you’re setting $row, maybe I missed it? Also, does the other database exist on the same server as the initial DB? You can simply do two connections. One to DB A and the other to DB B, two separate connection variables. Commented Sep 4, 2022 at 4:05
  • Yes the $row isn't showing on here. Most of the page I left out because it would just be clutter. The only part I'm struggling with is how to make it so that one line reads from a different database. Both databases are on the same server :) Commented Sep 5, 2022 at 15:05
  • Thanks for the clarification around it being on the same server, that’s important. I’ve posted an answer. Commented Sep 6, 2022 at 14:33

1 Answer 1

2

First, you can connect to two different databases on the same server by not specifying the database in the connection string:

$dbname1 = " ";
$dbname2 = " ";

// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

Second, use mysqli_select_db to change your default database.

Select your first database, write your first query, then select your second database, and write your second query. You can write it in ether object oriented style, or procedural style:

Object oriented style:

$conn -> select_db($dbname1);
//write your first query again database 1 here

$conn -> select_db($dbname2);
//write your second query again database 2 here

Procedural style:

mysqli_select_db($conn, $dbname1);
//write your first query again database 1 here

mysqli_select_db($conn, $dbname2);
//write your second query again database 2 here

Next, output your results using echo here, be sure to differentiate your results between the two queries using $row1 and $row2 for example.

Finally, close your connection:

mysqli_close($conn);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.