A link to the server could not be established

As per our discussion in the comments on your question, try changing things up so that inc.php is required in btw.php; btw.php is required in index.php rather than inc.php; and 'btw.php` is not required in inc.php. From reading php.net/manual/en/function.include.php, I think this might have to do with scope.

EDIT:

First off, the setup you have creates a custom database object class (DBConnection) which is used to interface with the database and execute queries. When you used mysql_query by itself, it did not have a database connection identifier from which to execute the query, since the DBConnection object abstracts that functionality in object methods. That is why you needed to use if (!$_DB->insert($insert)).

Secondly, and I'm not 100% on this, but essentially core the problem seems to have to do with the code in btw.php not "seeing" the database setup code. This could have been because of two things. First, the $_DB variable was defined after the btw.php code was required, and as a result, when the PHP interpreter parsed btw.php, $_DB had not yet been defined. The order if the requires and the database object definition matter. Secondly, and this is where I am a bit unsure, but I think there is a variable scope/access issue when requiring btw.php from within inc.php (where $_DB is defined) rather than requiring the database setup within btw.php. In other words, you had the code that uses the database object required in the script that defines it, rather than the database setup script (including the database object declaration) required within the code that uses it.

I hope that makes sense, please let me know if it is still confusing. I tend to have a problem explaining things concisely.

Tags:

Database

Php