Help needed getting two db connections on one page

Discussion in 'PHP' started by CodeMama, Nov 4, 2008.

  1. #1
    I can't seem to connect to two different db to connect on one page, I have tried several tutorials and different ways

    
    <?php
    include("inc/dbconn_openTest.php");
    
    if (empty($_SESSION['AdminLogin']) OR $_SESSION['AdminLogin'] <> 'OK' ){
    	header ("Location: LogOut.php");
    }
    
    if (isset($_GET['AdminID']) && !empty($_GET['AdminID'])){
    	$AdminID = $_GET['AdminID'];
    } else {
    	header ("Location: LogOut.php");
    }
    
        $query = "SELECT * FROM admin WHERE AdminID='$AdminID'";
        $result = mysql_query ($query);
    	$row = mysql_fetch_object ($result);
    ?>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Work Order System - Administrative Section</title>
    <LINK REL="STYLESHEET" HREF="inc/style.css">
    </head>
    
    <body bgcolor="#CCCCCC">
    <table width="170" border="0" cellpadding="0" cellspacing="0">
    	<tr>
    		<td align="center"><BR>
    		<div class="admin_Title" align="center">Work Order System Admin</div></td>
    	</tr>
    	<tr>
    		<td><BR><HR></td>
    	</tr>
    	<tr>
    		<td><div class="admin_link"><a href="Welcome.php?AdminID=<?php echo $AdminID; ?>" target="mainFrame">Home</a></div></td>
    	</tr>
    	<tr>
    		<td><HR></td>
    	</tr>	
    <?php
    	if ($row->AddWorkOrder == "YES") {
    ?>	
    	<tr>
    		<td><div class="admin_link"><a href="WorkOrder.php?AdminID=<?php echo $AdminID; ?>" target="mainFrame">Work Order Form</a></div></td>
    	</tr>
    	<tr>
    		<td><div class="admin_link"><a href="PD_Coupon.php?AdminID=<?php echo $AdminID; ?>" target="mainFrame">Planet Discover Coupon Form</a></div></td>
    	</tr>
    	<tr>
    		<td><div class="admin_link"><a href="PD_TextAd.php?AdminID=<?php echo $AdminID; ?>" target="mainFrame">Planet Discover Yellow Pages/Text Ad Form</a></div></td>
    	</tr>
    	<tr>
    		<td><div class="admin_link"><a href="PD_Enhanced.php?AdminID=<?php echo $AdminID; ?>" target="mainFrame">Planet Discover Enhanced Listing</a></div></td>
    	</tr>
    	<tr>
    		<td><div class="admin_link"><a href="Homescape_Builder.php?AdminID=<?php echo $AdminID; ?>" target="mainFrame">HomeScape Builder Form</a></div></td>
    	</tr>	
    	<tr>
    		<td><div class="admin_link"><a href="Homescape_SpecHome.php?AdminID=<?php echo $AdminID; ?>" target="mainFrame">HomeScape Builder Package Model Information</a></div></td>
    	</tr>	
    <?php
    	}
    ?>
    	<tr>
    		<td><HR></td>
    	</tr>
    <?php	
    	if ($row->SearchWorkOrder == "YES") {
    ?>	
    	<tr>
    		<td><div class="admin_link"><a href="SearchOrders.php?AdminID=<?php echo $AdminID; ?>" target="mainFrame">Search For Order</a></td>
    	</tr>
    <?php
    	}
    ?>
    	<tr>
    		<td><HR></td>
    	</tr>
    	
    	<tr>
    		<td align="center">
    		<div class="admin_Title" align="center">Art Upload Tools</div></td>
    	</tr>
    	<tr>
    		<td><HR><font color="#CC0000" size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>Testing Area!! <br> Please use regular ArtUpload Page until I give the word this is "LIVE"</strong></font></td>
    	</tr>
    	
    	<tr>
    		<td><div class="admin_link"><a href="../../ArtUpload/admin/AddNewArt.php?AdminID=<?php echo $AdminID; ?>" target="mainFrame">Upload Cust. Artwork</a></div></td>
    	</tr>
    	<tr>
    		<td><div class="admin_link"><a href="../../ArtUpload/admin/AddNewArtManual.php?AdminID=<?php echo $AdminID; ?>" target="mainFrame">Upload New Artwork - Manual</a></div></td>
    	</tr>
    	
    	
    	<tr>
    		<td><div class="admin_link"><a href="../../ArtUpload/admin/SearchProjects.php?AdminID=<?php echo $AdminID; ?>" target="mainFrame">Search For Art IO</a></td>
    	</tr>
    	<tr>
    		<td><div class="admin_link"><a href="../../ArtUpload/admin/ViewAllArt.php?AdminID=<?php echo $AdminID; ?>" target="mainFrame">View All Projects</a></div></td>
    	</tr>
    	<tr>
    		<td><div class="admin_link"><a href="../../ArtUpload/admin/ViewCompletedArt.php?AdminID=<?php echo $AdminID; ?>" target="mainFrame">View Completed Projects</a>
    <?php
       
    	$query = "SELECT * FROM admin WHERE AdminID='$AdminID'";
        $result = mysql_query ($query, $con2);
    	$row = mysql_fetch_object ($result);
    	$sql = "SELECT artwork.ArtID FROM artwork JOIN images ON (artwork.ArtID = images.ArtID) WHERE ";
    	$sql .= "artwork.Completed='YES' GROUP BY artwork.ArtID";
        $result = mysql_query ($sql, $con2);
    	echo "<span class=\"admin_Count\">(". ceil(mysql_num_rows($result)) .")</span>";
    	mysql_close();
    ?>	
    <!--was stuck here on snippet above this line-->
    		</div></td>
    	</tr>
    	<tr>
    		<td><HR></td>
    	</tr>
    	
    
    
    
    	
    	
    		
    <?php
    	if ($row->ViewNewOrders == "YES") {
    ?>	
    	<tr>
    		<td><div class="admin_link"><a href="ViewNewOrders.php?AdminID=<?php echo $AdminID; ?>" target="mainFrame">View New Orders</a>
    <?php
    	$sql = "SELECT WorkOrderID FROM workorders WHERE Status='New Order'";
        $result = mysql_query ($sql);
    	echo "<span class=\"admin_Count\">(". ceil(mysql_num_rows($result)) .")</span>";
    ?>		
    		</div></td>
    	</tr>
    <?php
    	}
    	if ($row->ViewNewArt == "YES") {
    ?>	
    	<tr>
    		<td><div class="admin_link"><a href="ViewNewArt.php?AdminID=<?php echo $AdminID; ?>" target="mainFrame">View New Art</a>
    <?php
    	$sql = "SELECT WorkOrderID FROM workorders WHERE Status='New Order' AND FormName='WorkOrder'";
        $result = mysql_query ($sql);
    	echo "<span class=\"admin_Count\">(". ceil(mysql_num_rows($result)) .")</span>";
    ?>		
    		</div></td>
    	</tr>
    <?php
    	}
    	if ($row->ViewPendingWorkOrders == "YES") {
    ?>	
    	<tr>
    		<td><div class="admin_link"><a href="ViewPendingOrders.php?AdminID=<?php echo $AdminID; ?>" target="mainFrame">View Pending Orders</a>
    <?php
    	$sql = "SELECT WorkOrderID FROM workorders WHERE Status='Pending'";
        $result = mysql_query ($sql);
    	echo "<span class=\"admin_Count\">(". ceil(mysql_num_rows($result)) .")</span>";
    ?>		
    		</div></td>
    	</tr>
    <?php
    	}
    	if ($row->ViewPendingArtwork == "YES") {
    ?>	
    	<tr>
    		<td><div class="admin_link"><a href="ViewPendingArt.php?AdminID=<?php echo $AdminID; ?>" target="mainFrame">View Pending Artwork</a>
    <?php
    	$sql = "SELECT WorkOrderID FROM workorders WHERE Status='WaitingOnArt'";
        $result = mysql_query ($sql);
    	echo "<span class=\"admin_Count\">(". ceil(mysql_num_rows($result)) .")</span>";
    ?>		
    		</div></td>
    	</tr>
    <?php
    	}
    	if ($row->ViewCompletedArt == "YES") {
    ?>	
    	<tr>
    		<td><div class="admin_link"><a href="ViewCompletedArt.php?AdminID=<?php echo $AdminID; ?>" target="mainFrame">View Completed Artwork</a>
    <?php
    	$sql = "SELECT WorkOrderID FROM workorders WHERE Status='ArtworkCompleted'";
        $result = mysql_query ($sql);
    	echo "<span class=\"admin_Count\">(". ceil(mysql_num_rows($result)) .")</span>";
    ?>		
    		</div></td>
    	</tr>
    <?php
    	}
    	if ($row->ViewCompletedWorkOrders == "YES") {
    ?>	
    	<tr>
    		<td><div class="admin_link"><a href="ViewCompletedOrders.php?AdminID=<?php echo $AdminID; ?>" target="mainFrame">View Completed Orders</a>
    <?php
    	$sql = "SELECT WorkOrderID FROM workorders WHERE Status='OrderCompleted'";
        $result = mysql_query ($sql);
    	echo "<span class=\"admin_Count\">(". ceil(mysql_num_rows($result)) .")</span>";
    ?>		
    		</div></td>
    	</tr>
    <?php
    	}
    	if ($row->ViewAllWorkOrders == "YES") {
    ?>	
    	<tr>
    		<td><div class="admin_link"><a href="ViewAllOrders.php?AdminID=<?php echo $AdminID; ?>" target="mainFrame">View All Orders</a>
    <?php
    	$sql = "SELECT WorkOrderID FROM workorders";
        $result = mysql_query ($sql);
    	echo "<span class=\"admin_Count\">(". ceil(mysql_num_rows($result)) .")</span>";
    ?>		
    		</div></td>
    	</tr>
    <?php
    	}
    ?>
    	<tr>
    		<td><HR></td>
    	</tr>
    <?php
    	if ($row->AddEditAdmin == "YES") {
    ?>	
    	<tr>
    		<td><div class="admin_link"><a href="AddAdmin.php?AdminID=<?php echo $AdminID; ?>" target="mainFrame">Add Admin</a></div></td>
    	</tr>
    	<tr>
    		<td><div class="admin_link"><a href="ViewAdmin.php?AdminID=<?php echo $AdminID; ?>" target="mainFrame">View Admin</a></div></td>
    	</tr>
    <?php
    	}
    ?>
    	<tr>
    		<td><div class="admin_link"><a href="ChangePassword.php?AdminID=<?php echo $AdminID; ?>" target="mainFrame">Change My Password</a></div></td>
    	</tr>
    	<tr>
    		<td><HR></td>
    	</tr>
    	<tr>
    		<td><div class="admin_link"><a href="LogOut.php" target="_parent">LogOut</a></div></td>
    	</tr>		
    </table>
    </body>
    </html>
    
    
    Code (markup):

     
    CodeMama, Nov 4, 2008 IP
  2. penalty

    penalty Member

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #2
    if you have two open mysql connections, every mysql_query() needs the attribut "resource" to know which connection it should take.

    open a mysql connection for example like:
    $db = mysql_connect(...);

    and another one like:
    $db1 = mysql_connect(...);

    then do a query on $db with: mysql_query($sql, $db)
    on $db1: mysql_query($sql, $db1)
     
    penalty, Nov 4, 2008 IP
  3. CodeMama

    CodeMama Member

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    Here is how I did the db conn file:
    
    <?php
    session_start();
    
    header("Cache-control: private");
    header("Expires: " . gmdate("D, d M Y H:i:s", time()) . " GMT"); 
    
    $db_host = "localhost";
    $db_user = "root";
    $db_pass = "paper";
    $db_name = "WorkOrder_DB";
    
    
        global $db_host;
        global $db_user;
        global $db_pass;
        global $db_name;
    	
    $con = mysql_connect($db_host,$db_user,$db_pass) OR die ("Could not connect to the server.");
    mysql_select_db($db_name, $con) OR die("Could not connect to the database.");
    ?>
    <?php
    session_start();
    $db2_host = "localhost";
    $db2_user = "root";
    $db2_pass = "paper";
    $db2_name = "Artupload2";
    
    
        global $db2_host;
        global $db2_user;
        global $db2_pass;
        global $db2_name;
    
    $con2 = mysql_connect($db2_host,$db2_user,$db2_pass) OR die ("Could not connect to the server.");
    mysql_select_db($db2_name, $con2) OR die("Could not connect to the database.");
    
    ?>
    
    
    
    Code (markup):
    I really need to figure this out...
    then when I tried to use the second db:
    
    <?php
       
    	$query = "SELECT * FROM admin WHERE AdminID='$AdminID'";
        $result = mysql_query ($query, $con2);
    	$row = mysql_fetch_object ($result);
    	$sql = "SELECT artwork.ArtID FROM artwork JOIN images ON (artwork.ArtID = images.ArtID) WHERE ";
    	$sql .= "artwork.Completed='YES' GROUP BY artwork.ArtID";
        $result = mysql_query ($sql, $con2);
    	echo "<span class=\"admin_Count\">(". ceil(mysql_num_rows($result)) .")</span>";
    	mysql_close();
    ?>	
    
    
    Code (markup):
     
    CodeMama, Nov 4, 2008 IP
  4. Christian Little

    Christian Little Peon

    Messages:
    1,753
    Likes Received:
    80
    Best Answers:
    0
    Trophy Points:
    0
    #4
    What error message was shown when you run this code?
     
    Christian Little, Nov 4, 2008 IP
  5. CodeMama

    CodeMama Member

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #5
    Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\Inetpub\wwwroot\WkOrderOnline_V\Menu.php on line 107

    I looked this up and it seems its because my connection is not there, its only connecting to one db at a time...
     
    CodeMama, Nov 4, 2008 IP
  6. CodeMama

    CodeMama Member

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #6
    I tried putting the db connections on two different include files and now I get:

    Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\Inetpub\wwwroot\WkOrderOnline_V\Menu.php on line 187

    Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\Inetpub\wwwroot\WkOrderOnline_V\Menu.php on line 187

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\Inetpub\wwwroot\WkOrderOnline_V
     
    CodeMama, Nov 4, 2008 IP
  7. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #7
    Here's the deal, I had this same issue with using 2 databases. You have to id which connection you are using, which I see in the 2nd code example you are doing. Good. The part which is hard to find, is that you have to call mysql_select_db(database.table,conn) before you do the mysql_query.

    $query = "SELECT * FROM admin WHERE AdminID='$AdminID'";
    mysql_select_db(database.admin,$con2);
    $result = mysql_query ($query, $con2);

    important part is that the select_db has to have the table name. I incorrectly assumed just calling mysql_select_db(database,conn) would work. Odd part is that mysql_select_db is flaky and it might work with just the database, conn on some servers.
     
    shallowink, Nov 4, 2008 IP