[SOLVED] connecting to a DB from inside a class
Posted: Sun Aug 21, 2005 8:30 pm
I'm using PHP 4 here and although I have used classes many times, I'm not being able to do it correctly regarding DB conectivity.
Here is the simplified code of my class:
Everytime I call the executeQry (after instantiating the class, of course), the echo gets executed, which means that the query was invalid.
How structure a class with database conectivity? Unfortunatly PEAR:DB is not an option.
Any help is greatly appreciated,
HEMOglobina
Here is the simplified code of my class:
Code: Select all
<?php
class App {
var $db = null;
function App() {
$this->db = $this->conectDB();
}
function conectDB() {
$dbname = 'myDB';
$dbserver = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$db = mysql_connect($dbserver, $dbuser, $dbpass) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($dbname, $db);
return $db;
}
function executeQry() {
$query = "SELECT * FROM sampletable";
$result = mysql_query($query,$this->db);
if (!$result) {
echo "query did not get executed";
}
}
}
?>How structure a class with database conectivity? Unfortunatly PEAR:DB is not an option.
Any help is greatly appreciated,
HEMOglobina