PNewCode Posted March 1, 2023 Share Posted March 1, 2023 (edited) 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 March 1, 2023 by PNewCode Quote Link to comment Share on other sites More sharing options...
Solution ginerjm Posted March 1, 2023 Solution Share Posted March 1, 2023 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 1 Quote Link to comment Share on other sites More sharing options...
Barand Posted March 1, 2023 Share Posted March 1, 2023 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, 1 Quote Link to comment Share on other sites More sharing options...
PNewCode Posted March 1, 2023 Author Share Posted March 1, 2023 @Barand and @ginerjm Thank you. This cleared up what I was stuck on. Awesome sauce is what you all are! Quote Link to comment Share on other sites More sharing options...
ginerjm Posted March 1, 2023 Share Posted March 1, 2023 So have you created new code yet? Quote Link to comment Share on other sites More sharing options...
jodunno Posted March 2, 2023 Share Posted March 2, 2023 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> Quote Link to comment Share on other sites More sharing options...
PNewCode Posted March 4, 2023 Author Share Posted March 4, 2023 @ginerjm and @jodunno thank you both. I haven't got to do any more with this task yet because I had to go spend some quality time with the doctor for a day but I will return to it soon And yes I look forward to learning more CSS, thank you for that example. I'll use that to mold more like it and learn from it. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted March 4, 2023 Share Posted March 4, 2023 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.