Jump to content

Using 2 databases in one loop


PNewCode
Go to solution Solved by ginerjm,

Recommended Posts

Hello everyone!

First, I must say that what I've been able to accomplish in the last few months from learning on here and research has given me a LOT of fun, which is what I need with my old bored self haha. So thank you all!

For my next learning experience, I am attempting to display 2 different "things" from 2 different databases. One is a text and one is an image.
I should let you know, that this is not an actual project. I'm doing this just for learning because I thought "hmmm, what if I did this..."

So no luck. Below is what I tried. Any thoughts?
(and before anyone asks, I did it this way because this was my best attempt. I am still figuring out the ways of the php magic :) )

 

<?php

// Database 1

  $hostname = "localhost";
  $username = "stuff";
  $password = "stuff";
  $dbname = "stuff"; 

// Database 2

  $hostnameB = "localhost";
  $usernameB = "stuff";
  $passwordB = "stuff";
  $dbnameB = "stuff";


// Both databases have the same table name 


   $conn = mysqli_connect($hostname, $username, $password, $dbname);
  if(!$conn){
    echo "Database connection error".mysqli_connect_error();
  }

   $connB = mysqli_connect($hostnameB, $usernameB, $passwordB, $dbnameB);
  if(!$connB){
    echo "Database connection error".mysqli_connect_error();
  }

  $sql = "SELECT * FROM tablename";
  $sqlB = "SELECT * FROM tablename";
  
$result = $conn->query($sql);
$resultB = $connB->query($sqlB);

if ($result->num_rows > 0){
  // output data of each row
  while($row = $result->fetch_assoc()) {

          $comment =  $row["comment"];
          
  }          
}
if ($resultB->num_rows > 0){
  // output data of each row
  while($row = $resultB->fetch_assoc()) {

          $imglink = "img/".$row['img'];

echo'
<table width="10%" border="0" cellspacing="0" cellpadding="10" align="center">
  <tr align="center"> 
    <td valign="middle"><img src="'.$imglink.'" width="50" height="50" /></td>                               
    <td valign="middle"><font face="Verdana, Arial, Helvetica, sans-serif" color="#CCCCFF"><b>'.$row["comment"].'</b></font></td>
  </tr>
</table>

'; 
  }
} else {
  echo "0 results";
}

?>

 

Edited by PNewCode
Link to comment
Share on other sites

  • Solution

One does not need to make 2 connections.  Simply add  the dbname to the tablename in your one query.

"select a.fld1, b.fld1,a.host, a.child, a...., b..., b... from db1.tablea a, db2.tableb b where a.key = b.key

  • Like 1
Link to comment
Share on other sites

Just to clarify...

A connection is to a server, not a table. The database name, when connecting, is just the default name to use if no db name is specified in the query. In this case, both connections are to "localhost" so a single connection will suffice.

If your tables were on different servers then you would need 2 connections and you would have to query the tables in separate queries,

  • Like 1
Link to comment
Share on other sites

Hello, i am just wondering why you do not experiment with pdo, a better database design and modern html/css concepts? I see a table and i wonder why you think that you need to use a table at all. I have attached a code example of a similar 'table' using div tags and a second one using a list. I recommend dropping the tables and moving to css. Also, try to learn reusable css design. 

<!DOCTYPE html>
<html>
<head>
  <title></title>
  <style>
    div { position: relative; margin: 0px 0px; padding: 0px 0px; box-sizing: content-box; }
    ul { position: relative; margin: 0px 0px; padding: 0px 0px; }
    li { list-style-type: none; position: relative; margin: 0px 0px; padding: 0px 0px; }
    .inlineBlock { display: inline-block; }
    .centered { text-align: center; }
    .valignmiddle { vertical-align: middle; }
    .pad10 { padding: 10px 10px; }
    .border2dc { border: solid 2px #dcdcdc; }
    .sans-serif { font-family: "Verdana", "Arial", "Helvetica", sans-serif; }
    .ccccff { color: #ccccff; } .000000 { color: #000000; }
    .embolden { font-weight: bold; }
    img.cellImage { display: inline-block; width: 50px; height: 50px; }
  </style>
</head>
<body>

    <div class="centered">
        <div class="inlineBlock border2dc valignmiddle"> 
            <div class="inlineBlock pad10 valignmiddle border2dc"><img class="cellImage" src="'.$imglink.'"></div>                               
            <div class="inlineBlock pad10 valignmiddle border2dc sans-serif embolden ccccff">$row["comment"]</div>
        </div>
    </div>
    <br>
    <ul class="centered">
        <li class="inlineBlock pad10 valignmiddle border2dc"><img class="cellImage" src="'.$imglink.'"></li>
        <li class="inlineBlock pad10 valignmiddle border2dc sans-serif embolden ccccff">$row["comment"]</li>
    </ul>

</body>
</html>

 

Link to comment
Share on other sites

Don't know about css recs to you since I didn't recommend any to you.  I think the other contributor to this post brought that up while trying to tell you that <table> tags our out-dated.  That is true and we are supposed to use CSS to design our html tables.  Guess what - I haven't seen the need to do that and perhaps someday I will have to change a whole lot of code.  In the meantime I will continue to use html tables whenever I need to display data in a tabular format.   And That's That.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.