I am working on a user database and have it set up so that the dropdown menus in my registration form take their values from tables in the database. The problem is that when I tested it out, the dropdowns were empty. Naturally the first thing I did was to go and check my connection. To connect to the database, I have a file called BaseDAO.php
which contains the following:
<?php
class BaseDAO {
const SQL_HOST = "localhost";
const SQL_USER = "user6538";
const SQL_PW = "sdWf234Fg";
const SQL_DB = "userDB";
protected $con = null;
function connect() {
$this->con = mysql_connect(self::SQL_HOST, self::SQL_USER, self::SQL_PW);
if(!$this->con) {die('Could not connect to MySQL host: ' . self::SQL_HOST . ' Error: ' . mysql_error()); }
echo "Connected!"; //NOTE: I only just added this to test it
mysql_select_db(self::SQL_DB);
}
function close() {
mysql_close($this->con);
}
}
?>
So to test it, I added echo "Hello World!";
outside of the class, just as a reference, then I added connect();
right below it. I opened my page and only saw the Hello World!
... So then I tried echo connect();
and again nothing. Then I tried this:
$baseDAO = new BaseDAO();
$baseDAO->connect();
And still there was nothing. Then I commented out the entire class and just added this:
<?php
//...
//Commented out class
//....
function connect() {
$con = mysql_connect("localhost", "user6538", "sdWf234Fg" );
if(!$con) { echo "3"; die('Could not connect: ' . mysql_error()); }
echo "Connected!";
}
echo "Hello World!";
connect();
?>
Lo and behold the output was this:
Hello World!Connected!
I don't understand why my class doesn't work... It explains why my dropdowns were not working, since their DAO files require my BaseDAO
.
Would someone care to enlighten me on what's going on here? Why won't my class work?
NOTE: I am using a friend's old project as a guide, so theoretically my BaseDAO.php
should work, since his worked.
EDIT: Corrected the arguments in the second connect() function