[RESOLVED] Having trouble when I have 2 databases in the same PHP file
Results 1 to 15 of 15

Thread: [RESOLVED] Having trouble when I have 2 databases in the same PHP file

  1. #1
    Senior Member
    Join Date
    May 2009
    Posts
    165

    resolved [RESOLVED] Having trouble when I have 2 databases in the same PHP file

    Hi:

    So I need to call 2 tables from 2 different databases...and I just cant get it to work.. i always get this error:

    Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /home/pdem/pdem.mattwatson.info/Display.php on line 86

    on the line that has:

    PHP Code:
    $num=mysql_numrows($result); 
    ...My question I guess is whether or not I am doing my query and stuff correctly... Here is some code.

    I have read the documentation, and lots of examples...i find a lot of people dont use:

    PHP Code:
    @mysql_select_db("pdem?database1",$projectDatabase) or die("Unable to select database"); 
    ...but it seems important to me..maybe you just do something else instead. i dont know :P

    Anyways here is the first part of my code (im sure everything needed is here)

    Thanks,
    Paul

    PHP Code:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>

    <head>
        <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
        <meta name="author" content="Toadums" />

        <title>REI Consultants Timesheet Query</title>
    </head>

    <body bgcolor="#DDE2CC"> <!-- set colour to r:221 g:226 b:204 (rgb codes)-->

    <img src="logo.jpg" /> <!-- link to the logo in same directory-->

    <br />
    <br />
    <br />

    <?php

    #save log in information for server
    $username="**";
    $password="**";
    $database="**";



    $timesheetDatabase mysql_connect("mysql.mattwatson.info",$username,$password);

    if(!
    $timesheetDatabase){
        
    die (
    "Unable to connect to server");

    }

    @
    mysql_select_db($database,$timesheetDatabase) or die("Unable to select database"); //<< DO I NEED THIS LINE??

    $projectDatabase mysql_connect("mysql.mattwatson.info",$username,$password);

    if(!
    $projectDatabase){
        
    die (
    "Unable to connect to server");

    }

    @
    mysql_select_db("pdem?database1",$projectDatabase) or die("Unable to select database");//<< DO I NEED THIS LINE??



    ?>
    <form name ="form2" action="<?php echo($_SERVER['PHP_SELF']); ?>" method="post">
    <table border="1" cellspacing="10" cellpadding="2">
    <tr>
    <th><font face="Arial, Helvetica, sans-serif">Date</font></th>
    <th><font face="Arial, Helvetica, sans-serif">Name</font></th>
    <th><font face="Arial, Helvetica, sans-serif">Hours</font></th>
    <th><font face="Arial, Helvetica, sans-serif">Phase</font></th>
    <th><font face="Arial, Helvetica, sans-serif">Project ID</font></th>
    <th><font face="Arial, Helvetica, sans-serif">Description</font></th>

    <th><font face="Arial, Helvetica, sans-serif">Project Name</font></th>
    <th><font face="Arial, Helvetica, sans-serif">Client</font></th>

    <th><font face="Arial, Helvetica, sans-serif">Invoiced</font></th>
    <th><font face="Arial, Helvetica, sans-serif">Type</font></th>
    </tr>

    <?
    $num 
    0;

    $startRow 0;
    $endRow 30;
    if(isset(
    $_POST['submit']) && $_POST['Name']!=""){
        
    $queryName $_POST['Name'];    

    $query="SELECT * FROM tblTimesheet WHERE Name= '$queryName' ORDER BY `tblTimesheet`.`Date` ASC LIMIT $startRow , $endRow  ";
    $result=mysql_query($query,$timesheetDatabase);
    }else{

    $query="SELECT * FROM tblTimesheet ORDER BY `tblTimesheet`.`Date` ASC LIMIT $startRow , $endRow ";
    $result=mysql_query($query,$timesheetDatabase);
    }

    $num=mysql_numrows($result); //<< WHERE I GET THAT ERROR

    $i=0;
    while (
    $i $num) {

    $Date=mysql_result($result,$i,"Date");
    $Hours=mysql_result($result,$i,"Hours");
    $Name=mysql_result($result,$i,"Name");
    $Phase=mysql_result($result,$i,"Phase");
    $Invoiced=mysql_result($result,$i,"Invoiced");
    $ProjectID=mysql_result($result,$i,"ProjectID");
    $Description=mysql_result($result,$i,"Description");
    $Type=mysql_result($result,$i,"Type");



    $nameQuery "SELECT ProjectName FROM tblProjects WHERE ProjectID = '$ProjectID'";
    $clientQuery "SELECT Client FROM tblProjects WHERE ProjectID = '$ProjectID'";

    $Pname mysql_query($nameQuery,$projectDatabase);
    $NameRow mysql_fetch_row($Pname);

    $Pclient mysql_query($clientQuery,$projectDatabase);
    $ClientRow mysql_fetch_row($Pclient);
    Hopefully that makes sense...If I am doing anything wrong, let me know :P

  2. #2
    Senior Member
    Join Date
    May 2009
    Posts
    165
    i realized that they are on the same SERVER and the lines with $timesheetDatabse = and $projectDatabase = ...arent needed...

    now I get an error that says "Table 'pdem?database1.tblTimesheet' doesn't exist" after trying to run a qery...it is trying to query from the WRONG DATABASE...but the line right before the query i have:
    PHP Code:
    @mysql_select_db("pdem_database1") or die("Unable to select database"); 
    ...this is the RIGHT database...

    (note that my databases are: pdem_database1 and pdem?database1)

    ...my friend who owns the mysql's laptop broke, so he cant create a new DB for me >.<

    If you know what I need to do to fix this...please let me know!!
    THanks

  3. #3
    Senior Member dagon's Avatar
    Join Date
    Nov 2001
    Posts
    6,178
    you can't have a db name with a question mark in it.

  4. #4
    Senior Member
    Join Date
    May 2009
    Posts
    165
    you serious?? dang. why did it work up til i tried using 2 databases...wierd.

    i guess ill hafa get my buddy over to my house so he can change the name on my comp >.<

    Thanks

  5. #5
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,432
    Quote Originally Posted by dagon View Post
    you can't have a db name with a question mark in it.
    Actually, there's nothing wrong with having a question mark in the name (or many other non-alphanumeric characters):
    Code:
    mysql> CREATE DATABASE `foo?bar`;
    Query OK, 1 row affected (0.01 sec)
    
    mysql> USE `foo?bar`;
    Database changed
    
    mysql> CREATE DATABASE `foo~!@#$%^&*`;
    Query OK, 1 row affected (0.02 sec)
    Last edited by bradgrafelman; 05-26-2009 at 02:24 AM.

  6. #6
    Senior Member
    Join Date
    May 2009
    Posts
    165
    Quote Originally Posted by bradgrafelman View Post
    Actually, there's nothing wrong with having a question mark in the name (or many other non-alphanumeric characters):
    oh. i was hoping for an easy solution! lol...

    do you know how i may fix this then?

    btw, i am using phpmyadmin for my server if that makes any difference...

  7. #7
    Senior Member dagon's Avatar
    Join Date
    Nov 2001
    Posts
    6,178
    bad me, i was thinking of _ and % as wildcards.

  8. #8
    Senior Member dagon's Avatar
    Join Date
    Nov 2001
    Posts
    6,178
    Quote Originally Posted by Toadums View Post
    btw, i am using phpmyadmin for my server if that makes any difference...
    I really hope not! that's a php script not a database server

  9. #9
    Senior Member
    Join Date
    May 2009
    Posts
    165
    uh...whats that mean? >.<

  10. #10
    Senior Member dagon's Avatar
    Join Date
    Nov 2001
    Posts
    6,178
    try changeing your queries so you use

    database.table

    ie


    $query="SELECT * FROM pdem_database1.tblTimesheet ... etc

  11. #11
    Senior Member
    Join Date
    May 2009
    Posts
    165
    gah..still not working...now the form just freezes and doesnt load any rows (just headers) ...i think ive had enough of this for one day >.<

  12. #12
    Senior Member
    Join Date
    May 2009
    Posts
    165
    Quote Originally Posted by Toadums View Post
    gah..still not working...now the form just freezes and doesnt load any rows (just headers) ...i think ive had enough of this for one day >.<
    btw. ALL this trouble started when i tried adding "next/previous 30 record" buttons...

    the query issue is sorted out...prolly not actually how it is supposed to be done..but works..

    It really looks like there is an issue with the ? because the one that has _ works fine...but both work now

    Now i just gota sort out this button crap...that will be tomorrow..then i should be DONE!! (with this one...go expense sheet next >.<)

    Thanks for all your help

  13. #13
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,432
    General tip I can offer when debugging SQL errors is changing things like this:
    PHP Code:
    $result=mysql_query($query,$timesheetDatabase); 
    to this:
    PHP Code:
    $result=mysql_query($query,$timesheetDatabase) or die("MySQL error: " mysql_error() . "<hr>Query: $query"); 
    to get more debugging info.

    Also, as dagon suggested, you should use the `db_name`.table_name syntax in your queries and get rid of the second connection to the server - there's no need for it since you're connecting to the same server with the same credentials.

    EDIT: Just to clarify, I used backticks around the database name in my suggestion above because the question mark, while a legal character in a database name, will require you to use them so that MySQL knows you're referencing a database name (aka an identifier).

  14. #14
    Senior Member
    Join Date
    May 2009
    Posts
    165
    Quote Originally Posted by bradgrafelman View Post
    Also, as dagon suggested, you should use the `db_name`.table_name syntax in your queries and get rid of the second connection to the server

    ah crap..those arent apostrophes, they are the little thingy next to 1 >.< ! lol

    ...works!!! haha thanks :P

  15. #15
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,432
    Don't forget to mark this thread resolved (if it is) using the link on the Thread Tools menu.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •