You are here: Database Class Help « PHP « IT news, forums, messages
Database Class Help

Posted by "Ian Barnes" on 10/05/34 11:33

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация