|
-
Apr 5th, 2007, 03:17 PM
#1
Thread Starter
Lively Member
[RESOLVED] Multiple databases
I am trying to display different databases on my webpage.
Here is the syntax I am imputting:
Here is for one database and it works.
Code:
<?
mysql_connect("localhost", "rgross_rgross", "pwd");
mysql_select_db("rgross_mlbstandings2007");
?>
Now to have another database work do I just do this:
Code:
<?
mysql_connect("localhost", "rgross_rgross", "pwd");
mysql_select_db("rgross_mlbstandings2007");
?>
<?
mysql_connect("localhost", "rgross_robgross", "pwd");
mysql_select_db("rgross_nyylineup2k7");
?>
-
Apr 5th, 2007, 03:23 PM
#2
Re: Multiple databases
To use multiple connections you must store the connection handles.
Also, it's bad style to use <?. For maximum compatibility, always use <?php.
PHP Code:
<?php
$conn1 = mysql_connect("localhost", "rgross_rgross", "pwd");
mysql_select_db('rgross_mlbstandings2007', $conn1);
$conn2 = mysql_connect("localhost", "rgross_robgross", "pwd");
mysql_select_db('rgross_nyylineup2k7', $conn2);
?>
-
Apr 5th, 2007, 03:28 PM
#3
Thread Starter
Lively Member
Re: Multiple databases
awesome...
thanks for the tip.....
Here is the page code after I entered your code.
Here is the actual page.
-
Apr 5th, 2007, 03:30 PM
#4
Re: Multiple databases
Ah, you need to pass the connection handle to mysql_query() as well.
-
Apr 5th, 2007, 03:36 PM
#5
Thread Starter
Lively Member
Re: Multiple databases
sorry new to php coding...
you need to pass the connection handle to mysql_query()
What does that mean?
-
Apr 5th, 2007, 03:50 PM
#6
Hyperactive Member
Re: Multiple databases
$result = mysql_query("SELECT * FROM table", $conn1);
$conn1 referring to which connection you want to use.
-
Apr 5th, 2007, 03:58 PM
#7
Thread Starter
Lively Member
Re: Multiple databases
I must be doing something wrong.....
PHP Code:
<?php
$result = mysql_query("SELECT * FROM lineup", $conn2);
while( $row = mysql_fetch_assoc($sql) ) {
echo '
<td>' . $row['player'] . '</td>
<td>' . $row['position'] . '</td>
</tr>';
}
?>
PHP Code:
<?
$result = mysql_query("SELECT * FROM ALEast", $conn1);
$color_num = "0";
while ($td = mysql_fetch_array($get_teamdata)) { ?>
<tr class="<? if ($color_num == "1") { ?>B<? } else { ?>A<? } ?>">
<td class="style26"><img src="<?=$td['logo']; ?>" border="0" width="25" height="25" alt="<?=$td['Team']; ?>" /></td>
<td id="RHE"><?=$td['W']; ?></td>
<td id="RHE"><?=$td['L']; ?></td>
<td id="RHE"><?=$td['PCT']; ?></td>
<td id="RHE"><?=$td['GB']; ?></td>
</tr>
<? if ($color_num == "0") { $color_num++; } else { $color_num--; } } ?>
-
Apr 5th, 2007, 04:19 PM
#8
Hyperactive Member
PHP Code:
<?php
$result = mysql_query("SELECT * FROM lineup", $conn2);
while( $row = mysql_fetch_array($result) ) {
echo '
<td>' . $row['player'] . '</td>
<td>' . $row['position'] . '</td>
</tr>';
}
?>
try this. notice how I changed $sql to $result.
$result is your data being returned, so when fetching the data you have to fetch the variable that you used when querying (which you used $result)
PHP Code:
<?
$result = mysql_query("SELECT * FROM ALEast", $conn1);
$color_num = "0";
while ($td = mysql_fetch_array($result)) { ?>
<tr class="<? if ($color_num == "1") { ?>B<? } else { ?>A<? } ?>">
<td class="style26"><img src="<?=$td['logo']; ?>" border="0" width="25" height="25" alt="<?=$td['Team']; ?>" /></td>
<td id="RHE"><?=$td['W']; ?></td>
<td id="RHE"><?=$td['L']; ?></td>
<td id="RHE"><?=$td['PCT']; ?></td>
<td id="RHE"><?=$td['GB']; ?></td>
</tr>
<? if ($color_num == "0") { $color_num++; } else { $color_num--; } } ?>
Same deal here, if using $result to store the data, you need to use it when fetching array.
Last edited by penagate; Apr 6th, 2007 at 01:23 PM.
Reason: merged double post
-
Apr 5th, 2007, 04:33 PM
#9
Thread Starter
Lively Member
Re: Multiple databases
excellant...
thanks for your help! I saw what I was doing wrong!
I had a $result and $sql.
Again thanks!
-
Apr 5th, 2007, 04:34 PM
#10
Hyperactive Member
Re: Multiple databases
No problem. Don't forget to mark your thread resolved.
BTW - your site looks nice
-
Apr 5th, 2007, 04:58 PM
#11
Thread Starter
Lively Member
Re: Multiple databases
I am trying to push my luck....sorry
trying to add the 5 Highest Payroll database now. "conn3"
This looks right?
PHP Code:
<?php
$conn1 = mysql_connect("localhost", "rgross_rgross", "***");
mysql_select_db('rgross_mlbstandings2007', $conn1);
$conn2 = mysql_connect("localhost", "rgross_robgross", "***");
mysql_select_db('rgross_nyylineup2k7', $conn2);
$conn3 = mysql_connect("localhost", "rgross_robgross", "***");
mysql_select_db('rgross_nyyhighestsalaries', $conn3);
?>
PHP Code:
<?php
$result = mysql_query("SELECT * FROM ALEast", $conn1);
$color_num = "0";
while ($td = mysql_fetch_array($result)) { ?>
<tr class="<? if ($color_num == "1") { ?>B<? } else { ?>A<? } ?>">
<td class="style26"><img src="<?=$td['logo']; ?>" border="0" width="25" height="25" alt="<?=$td['Team']; ?>" /></td>
<td id="RHE"><?=$td['W']; ?></td>
<td id="RHE"><?=$td['L']; ?></td>
<td id="RHE"><?=$td['PCT']; ?></td>
<td id="RHE"><?=$td['GB']; ?></td>
</tr>
<? if ($color_num == "0") { $color_num++; } else { $color_num--; } } ?>
PHP Code:
<?php
$result = mysql_query("SELECT * FROM lineup", $conn2);
while( $row = mysql_fetch_array($result) ) {
echo '
<td>' . $row['player'] . '</td>
<td>' . $row['position'] . '</td>
</tr>';
}
?>
PHP Code:
<?php
$result = mysql_query("SELECT * FROM salaries", $conn3);
while( $row = mysql_fetch_array($result) ) {
echo '
<td>' . $row['player'] . '</td>
<td>' . $row['salary'] . '</td>
</tr>';
}
?>
-
Apr 5th, 2007, 05:52 PM
#12
Hyperactive Member
Re: Multiple databases
Not sure where it's going wrong but what you need to do is check and make sure you have records before trying to get your array.
PHP Code:
<?php
$result = mysql_query("SELECT * FROM salaries", $conn3);
if (mysql_num_rows($result) > 0){
while( $row = mysql_fetch_array($result) ) {
echo '
<td>' . $row['player'] . '</td>
<td>' . $row['salary'] . '</td>
</tr>';
}
} else {
echo "No records found";
}
?>
Basically what we are doing here is running the query and checking to make sure there are rows being returned. (means records were found) If it returns 0 then you have no records.
Also - Your site is giving the error at line 44 and the section you have it under is "Projected Lineup", but you are saying the error is with highest paid players.
double check your db and table names. and change your query line to this:
PHP Code:
$result = mysql_query("SELECT * FROM salaries", $conn3) or die(mysql_error());
This is good for debugging.
Last edited by zalez; Apr 5th, 2007 at 05:55 PM.
-
Apr 5th, 2007, 06:25 PM
#13
Thread Starter
Lively Member
Re: Multiple databases
now it seems that the highest payroll works except that the data I have in my database isn't in ascending order when being displayed.
The lineup issue still exists....
-
Apr 5th, 2007, 07:13 PM
#14
Hyperactive Member
Re: Multiple databases
did you change your query like my previous post suggestion?
Also why are you using multiple db's when you can create multiple tables instead and use one db connection?
-
Apr 5th, 2007, 07:44 PM
#15
Thread Starter
Lively Member
Re: Multiple databases
thats what I am doing now. Its getting far too complicated....
lol
-
Apr 6th, 2007, 12:49 AM
#16
Re: Multiple databases
as a quick aside: you're using the "ID" parameter improperly. the "ID" parameter for an XHTML object is a unique property -- meaning that no other object in that page may use it. therefor, your TDs that all have an ID of "RHE" are incorrect. instead, you should change it to a "class" parameter and likewise in your document. classes are used for multiple objects within the same XHTML document. structure might be something like this:
Code:
CSS:
---------------------
#container {
width: 75%;
background-color: #ff0;
}
#container .column { width: 25%; }
#container .left { float: left; }
#container .right { float: right; }
XHTML:
---------------------
<div id="container">
<div class="column left">
<!-- left side -->
</div>
<div class="column right">
<!-- right side -->
</div>
</div>
in the above case, our ID is a unique container that will hold everything in the page. then, there are two column classes both with a 'sub-class' that defines which side it shall be displayed on.
anyway, it's not a huge deal, but if you're looking to move into mainstream web design, you will want to make sure you can abide by most XHTML standards so that a browser doesn't misinterpret what you're trying to tell it to do! at the very least, it can give you the feeling that you "did it right."
-
Apr 6th, 2007, 01:06 AM
#17
Re: Multiple databases
 Originally Posted by rgyankees23
now it seems that the highest payroll works except that the data I have in my database isn't in ascending order when being displayed.
The lineup issue still exists....
You are aware that a single database can have multiple tables, right?
-
Apr 6th, 2007, 01:20 PM
#18
Thread Starter
Lively Member
Re: Multiple databases
I took your advise....
Problem solved! Thanks for all your help!
Last edited by rgyankees23; Apr 6th, 2007 at 02:50 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|