Results 1 to 18 of 18

Thread: [RESOLVED] Multiple databases

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2002
    Location
    New York
    Posts
    74

    Resolved [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");
    ?>

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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);
    ?>

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2002
    Location
    New York
    Posts
    74

    Re: Multiple databases

    awesome...

    thanks for the tip.....

    Here is the page code after I entered your code.

    Here is the actual page.

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Multiple databases

    Ah, you need to pass the connection handle to mysql_query() as well.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2002
    Location
    New York
    Posts
    74

    Re: Multiple databases

    sorry new to php coding...

    you need to pass the connection handle to mysql_query()
    What does that mean?

  6. #6
    Hyperactive Member
    Join Date
    Dec 2004
    Posts
    326

    Re: Multiple databases

    $result = mysql_query("SELECT * FROM table", $conn1);

    $conn1 referring to which connection you want to use.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Dec 2002
    Location
    New York
    Posts
    74

    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--; } } ?>

  8. #8
    Hyperactive Member
    Join Date
    Dec 2004
    Posts
    326
    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

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Dec 2002
    Location
    New York
    Posts
    74

    Re: Multiple databases

    excellant...

    thanks for your help! I saw what I was doing wrong!

    I had a $result and $sql.

    Again thanks!

  10. #10
    Hyperactive Member
    Join Date
    Dec 2004
    Posts
    326

    Re: Multiple databases

    No problem. Don't forget to mark your thread resolved.

    BTW - your site looks nice

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Dec 2002
    Location
    New York
    Posts
    74

    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>'
    ;  

    ?>

  12. #12
    Hyperactive Member
    Join Date
    Dec 2004
    Posts
    326

    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.

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Dec 2002
    Location
    New York
    Posts
    74

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

  14. #14
    Hyperactive Member
    Join Date
    Dec 2004
    Posts
    326

    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?

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Dec 2002
    Location
    New York
    Posts
    74

    Re: Multiple databases

    thats what I am doing now. Its getting far too complicated....

    lol

  16. #16
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    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."

  17. #17
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Multiple databases

    Quote 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?
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Dec 2002
    Location
    New York
    Posts
    74

    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
  •  



Click Here to Expand Forum to Full Width