0

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

Laya302
  • 77
  • 2
  • 7
  • add public in front of your functions `public function connect()` – Daan Aug 14 '14 at 13:55
  • Still didn't work... – Laya302 Aug 14 '14 at 13:57
  • You have commented out the class, but still in your function you are using `self::SQL_HOST`, how is it working? It surely calling the function, but your arguments have no meaning to mysql_connect function –  Aug 14 '14 at 13:59
  • public $con ? and use MySQLi or PDO instead off mysql cause its deprecated. – Webice Aug 14 '14 at 13:59
  • Oops, sorry that was just a copy-paste error. I actually replaced it with the actual localhost, username, etc... I will correct. – Laya302 Aug 14 '14 at 14:00
  • I tested your code (with class) and it works. Could you please check your php error.log file and see what error you are getting. Also please do not use mysql_* functions as they are deprecated. Instead use either mysqli or PDO. –  Aug 14 '14 at 14:13
  • What version of PHP are you using? If your friend's PHP version is higher than your, then that could explain it. Plus, if error reporting isn't on, do. http://php.net/manual/en/function.error-reporting.php which may signal possible errors. – Funk Forty Niner Aug 14 '14 at 14:14
  • Tested fine on PHP Version 5.2.17 => `Connected!` - Check your DB credentials also, while using `error_reporting(E_ALL); ini_set('display_errors', 1);` if you're not already doing so. This http://stackoverflow.com/q/151969/ may also be of help. – Funk Forty Niner Aug 14 '14 at 14:26
  • @Fred-ii- I doubt he used a higher version, his project is from last year. PHP Version 5.3.2-1ubuntu4.14 – Laya302 Aug 14 '14 at 14:29
  • @Fred-ii- I'm not sure how to use `error_reporting(E_ALL); ini_set('display_errors', 1);` ? Where do I put that? – Laya302 Aug 14 '14 at 14:30
  • @Fred-ii- I did `phpinfo();` – Laya302 Aug 14 '14 at 14:31
  • Place that underneath your opening ` – Funk Forty Niner Aug 14 '14 at 14:31
  • @Fred-ii- ok so i got this: `Fatal error: Using $this when not in object context on line 15` I had no idea about using error reporting, I was coding blindly :S Thanks! – Laya302 Aug 14 '14 at 14:34
  • You're welcome. Now you know what to go after ;) – Funk Forty Niner Aug 14 '14 at 14:36
  • 1
    Thanks so much! Lifesaver! – Laya302 Aug 14 '14 at 14:37
  • You're very much welcome. Using [error reporting](http://php.net/manual/en/function.error-reporting.php) is definitely a tool worthy for developers, *cheers* ;) – Funk Forty Niner Aug 14 '14 at 14:41

2 Answers2

0
class BaseDAO {

    private static $SQL_HOST = "localhost";
    private static $SQL_USER = "user6538";
    private static $SQL_PW = "sdWf234Fg";
    private static $SQL_DB = "userDB";

    protected static $con = null;

    public static function connect() {
        self::con = mysql_connect(self::SQL_HOST, self::SQL_USER, self::SQL_PW);
        if(!self::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);
    }

    public static function close() {
       mysql_close(self::con);
    }

}

To connect do the following no need to create an instance of the class because its static:

BaseDAO::connect();
Daan
  • 12,099
  • 6
  • 34
  • 51
0

The return value of mysql_connect is FALSE in an error case. 0 is a positive value. To be sure, make this change:

    if(self::con === FALSE) {die('Could not connect to MySQL host: ' . self::SQL_HOST . ' Error: ' . mysql_error()); }

Your second SQL, where you did remove the CLASS parts, cannot work, but it strangely does.

$con = mysql_connect(self::SQL_HOST, self::SQL_USER, self::SQL_PW);

You do not have the self:: constants defined if you class does not exist. PHP should have produced an error message in this case. So your description is wrong and missing something.

Franz Holzinger
  • 913
  • 10
  • 20