PHP

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • Database Class Help

    2 answers - 1685 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    Hi,
    We have a database class that we want to use to connect to multiple
    databases at the same time using different variables.
    Here is part of the db class:
    class db {
    var $dbh = "";
    var $q = "";
    function db($db_host, $db_user, $db_pass, $db_name) {
    $this->dbh = @mysql_pconnect($db_host, $db_user, $db_pass);
    $this->select_db($db_name);
    }
    function select_db($db) {
    if (!@mysql_select_db($db, $this->dbh)) {
    die("<em>Cannot select database because:</em></p>\n<p><code>" .
    mysql_error() . "</code>");
    }
    }
    function num_rows($_q) {
    if (!$this->q = mysql_query($_q, $this->dbh)) {
    die("Invalid query \"<code>" . $_q . "</code>\"</p>\n<p><code>" .
    mysql_error() . "</code>");
    } else {
    return mysql_num_rows($this->q);
    }
    @mysql_free_result($this->q);
    }
    }
    In my php file I say something like
    <?php
    Include ( 'db.php' );
    $db_1 = new db('server','username','password','db1');
    $db_2 = new db('server','username','password','db2');
    Echo $db_1->num_rows('select * from table1');
    Echo $db_2->num_rows('select * from table2');
    ?>
    Now that happens is when I try and do the query for db1, I get told that
    db2.table1 doesn't exist. I can swop anything around and it doesn't seem to
    work for one of the classes.
    Does anyone have any idea as to why this is happening?
    Thanks a lot,
    Ian
  • No.1 | | 254 bytes | |

    12/1/05, Albert <albert (AT) fastworx (DOT) comwrote:
    The downside of this is that you do not have persistent connections which
    last beyond the end of the script.
    Maybe it's not a real downside :)
    and
    -ahmed
  • No.2 | | 511 bytes | |

    Echo should be echo and also are you sure that you have those tables and
    databases? make some DB's called table1 and table 2 and in that make tables
    db1 and db2 that might work

    12/1/05, Ahmed Saad <ahmed.lists (AT) gmail (DOT) comwrote:

    12/1/05, Albert <albert (AT) fastworx (DOT) comwrote:
    The downside of this is that you do not have persistent connections
    which
    last beyond the end of the script.

    Maybe it's not a real downside :)

    and

    -ahmed

Re: Database Class Help


max 4000 letters.
Your nickname that display:
In order to stop the spam: 2 + 1 =
QUESTION ON "PHP"

EMSDN.COM