Click to See Complete Forum and Search --> : [RESOLVED] Multiple databases
rgyankees23
Apr 5th, 2007, 03:17 PM
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.
<?
mysql_connect("localhost", "rgross_rgross", "pwd");
mysql_select_db("rgross_mlbstandings2007");
?>
Now to have another database work do I just do this:
<?
mysql_connect("localhost", "rgross_rgross", "pwd");
mysql_select_db("rgross_mlbstandings2007");
?>
<?
mysql_connect("localhost", "rgross_robgross", "pwd");
mysql_select_db("rgross_nyylineup2k7");
?>
penagate
Apr 5th, 2007, 03:23 PM
To use multiple connections you must store the connection handles.
Also, it's bad style to use <?. For maximum compatibility, always use <?php.
<?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);
?>
rgyankees23
Apr 5th, 2007, 03:28 PM
awesome...
thanks for the tip.....
Here (http://robbyzworld.com/personal/Yankees/yankeez.txt) is the page code after I entered your code.
Here (http://robbyzworld.com/personal/Yankees/yankeez.php) is the actual page.
penagate
Apr 5th, 2007, 03:30 PM
Ah, you need to pass the connection handle to mysql_query() as well.
rgyankees23
Apr 5th, 2007, 03:36 PM
sorry new to php coding...
you need to pass the connection handle to mysql_query()
What does that mean?
zalez
Apr 5th, 2007, 03:50 PM
$result = mysql_query("SELECT * FROM table", $conn1);
$conn1 referring to which connection you want to use.
rgyankees23
Apr 5th, 2007, 03:58 PM
I must be doing something wrong.....
<?php
$result = mysql_query("SELECT * FROM lineup", $conn2);
while( $row = mysql_fetch_assoc($sql) ) {
echo '
<td>' . $row['player'] . '</td>
<td>' . $row['position'] . '</td>
</tr>';
}
?>
<?
$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--; } } ?>
zalez
Apr 5th, 2007, 04:19 PM
<?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)
<?
$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.
rgyankees23
Apr 5th, 2007, 04:33 PM
excellant...
thanks for your help! I saw what I was doing wrong!
I had a $result and $sql.
Again thanks!
zalez
Apr 5th, 2007, 04:34 PM
No problem. Don't forget to mark your thread resolved.
BTW - your site looks nice :)
rgyankees23
Apr 5th, 2007, 04:58 PM
I am trying to push my luck....sorry
trying to add the 5 Highest Payroll database now. "conn3"
This looks right?
<?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
$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
$result = mysql_query("SELECT * FROM lineup", $conn2);
while( $row = mysql_fetch_array($result) ) {
echo '
<td>' . $row['player'] . '</td>
<td>' . $row['position'] . '</td>
</tr>';
}
?>
<?php
$result = mysql_query("SELECT * FROM salaries", $conn3);
while( $row = mysql_fetch_array($result) ) {
echo '
<td>' . $row['player'] . '</td>
<td>' . $row['salary'] . '</td>
</tr>';
}
?>
zalez
Apr 5th, 2007, 05:52 PM
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
$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:
$result = mysql_query("SELECT * FROM salaries", $conn3) or die(mysql_error()); This is good for debugging.
rgyankees23
Apr 5th, 2007, 06:25 PM
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....
zalez
Apr 5th, 2007, 07:13 PM
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?
rgyankees23
Apr 5th, 2007, 07:44 PM
thats what I am doing now. Its getting far too complicated....
lol
kows
Apr 6th, 2007, 12:49 AM
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:
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."
visualAd
Apr 6th, 2007, 01:06 AM
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?
rgyankees23
Apr 6th, 2007, 01:20 PM
I took your advise....
Problem solved! Thanks for all your help!
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.