Jump to content

accessing 2 databases


kirkh34

Recommended Posts

hello,

 

im setting up a messaging system where users can create a profile and exchange messages...

 

im on the inbox page where i'm listing the messages with the name, a thumbnail of the sender, the subject, and date. i have two databases right now, one with the member info including their name and origin and so on, i have another database for the messages... in the db "messages" it has a "from_user" and "to_user", when you go t your inbox it matches the session id to the "to_user".  I am trying to list the persons name in the table of who it's from... but "from_user" is just an id of the user... i need to access the db of mymembers that has the name matched with the id... i'm not sure how to access both databases... , here's the code

<?php

session_start();
include_once "scripts/connect_to_mysql.php";

$user= $_SESSION['id'];

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<style type="text/css">
#customers
{
font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;
width:100%;
padding: 0px;

}
#customers td, #customers th 
{
font-size:1em;
border-top:1px solid #98bf21;
border-bottom:1px solid #98bf21;

padding:3px 7px 2px 7px;
}
#customers th 
{
font-size:1.1em;
text-align:left;
padding-top:5px;
padding-bottom:4px;
background-color:#A7C942;
color:#ffffff;
}
#customers tr.alt td 
{
color:#000000;
background-color:#EAF2D3;
}
</style>


</head>

<body>


<table id="customers">
<tr>
  <th>Inbox</th>
</tr>
<tr>

<td>Picture</td>
<td>Name</td>
<td>Subject</td>
<td>Date</td>
</tr>

<?php




$sql = mysql_query("SELECT * FROM messages WHERE to_user ='$user'")  
or die (mysql_error()); 

while ($row = mysql_fetch_array($sql))
{

$from_id = $row["from_user"];

$check_pic = "memberFiles/$from_id/image01.jpg";
$default_pic = "memberFiles/0/image01.jpg";
if (file_exists($check_pic)) {
    $from_user = "<img src=\"$check_pic\" width=\"50px\" />"; // forces picture to be 100px wide and no more
} else {
$from_user = "<img src=\"$default_pic\" width=\"50px\" />"; // forces default picture to be 100px wide and no more
}

echo "<tr>";


echo "<td>";
echo "$from_user";
echo "</td>";

echo "<td>";
echo "$firstname";
echo "</td>";

echo "<td>";
echo $row["message_title"];
echo "</td>";

echo "<td>";
echo $row["message_title"];
echo "</td>";

echo "</tr>";

} //close while
?>
</table>







</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/199705-accessing-2-databases/
Share on other sites

Um, i would highly reccommend using a single database. having to split queries in such a bad way can only cause you major headaches in the future.

 

Trust me, put your message and member tables in the same database. Only ever use a single database for a single website.

 

-cb-

okay, well it sounds easier that way, i did a tutorial on a user system and messaging system and he had multiple databases... but i just got it to work by accessing the messages db just for the id, then pulling from mymembers and then back to pulling from messages for the table, so idk, you are right it would prob be easier w/ just one db

 

oops i noticed something, i've been saying db when i just mean tables in the db, lol i am a noooob, anyway i got it to work

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.