Re: Two MySQL connections in one script not working as expected

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




On Sep 27, 2005, at 3:42 PM, Andy Pieters wrote:


Hi

Without you actually showing us these class files we can only guess but a
common mistake is this:

mysql_open(connection details)
mysql_query(query)

In those cases the last opened handle is used. To prevent this, use this
syntax

$db1=mysql_open(connection for db1);
$db2=mysql_open(connection for db2);

mysql_query($db1,$query_db_1);
mysql_query($db2,$query_db_2);

If you have used this syntax then check your class if it is using a global variable to hold the database handle and if it does make it a class variable
instead

Instead of

$db=null

class db
{function db()
 {$GLOBALS['db']=mysql_open(...

do instead

class db
{var $db=null;
 function db()
 {$this->db=mysql_open

That way you can instanciate as many instances of the class as you like and
each will have its own database handle.

HTH


Andy



What I have is more like this:

class db {
  var $dbpath = "localhost";
  var $dbname = "testdb";
  var $dblogin = "test";
  var $dbpass = "test";

  function db() {
$link = mysql_connect($this->dbpath, $this->dblogin, $this- >dbpass) or die ('Not Connected: ' . mysql_error()); mysql_select_db($this->dbname, $link) or die ('Can\'t use this database: ' . mysql_error());
  }

  function retrieveData( $sql ) {
$rs = mysql_query( $sql ) or die("Invalid query: " . mysql_error ());
    // if no result, return null
    if (($rs == null) || (mysql_num_rows($rs) == 0)) {
      return null;
    } else {
      return ( $rs );
    }
  }

  function insertData( $sql ) {
    mysql_query( $sql );
    // return new id if insert is successful
    if ( mysql_affected_rows() > 0 ) {
      return ( mysql_insert_id() );
    } else {
      return null;
    }
  }

  function updateData( $sql ) {
    $rs = mysql_query( $sql );
    if (mysql_affected_rows() > 0) {
      return ( $rs );
    } else {
      return null;  // no changes were made
    }
  }
}


I then have another class with a different name and I changed the names of the functions as well. I have another set of class files that contain my various queries etc.

Thanks for any help.
Charles

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux