Hi,
I am trying to encapsulate database connection code into a PHP class file. I am able to run this program fine if the code is inside a .phtml document, but as soon as I move the code into an .obj file and call it using a .phtml document, the mysql_select_db command stops working. Can anybody tell me what I'm doing wrong?!
Here is the code:
//DBConnection.obj - the class file
class DBConnection
{
var $hostname = "localhost";
var $username = "origin";
var $password = "secret";
var $dbName = "oxc_db";
var $numRows = 0;
function openConnection()
{
$db = MYSQL_CONNECT("$hostname", "$username", "$password") OR DIE("Unable to connect to database");
mysql_select_db(this->dbName) or die( "Unable to select database"); //This is the line that causes the error
}
}
// DBTest.phtml - this is the php code that calls the DBConnection.obj class
<?
include "DBConnection.obj";
?>
<Table>
<?
$queryString = "select * from users";
$dbConn = new DBConnection();
$dbConn -> openConnection();
?>
</table>
</BODY>
</HTML>
When I run this program, I keep receiving the error "Unable to select database" which is a result of the mysql_select_db() function.