Jump to content

Database Connection Issue


Rabastan

Recommended Posts

I am new to PHP/MySQL and I am experimenting with separating things to different pages and Include/Requiring them when needed. I am trying to make a database connection, but keep getting my "die" error.

 

OK Here is what I have:

 

At the top of the page I am trying to load I have:

require('app/application.php');

 

I have a page called "app.php"

// set the level of error reporting
  error_reporting(E_ALL & ~E_NOTICE);

// load file, folder and database parameters
   include('app/config.php');

// include the list of project filenames
  require(DIR_APPLICATION . 'filenames.php');

// include the list of project database tables
  require(DIR_APPLICATION . 'db_tables.php');

// include the database functions
  require(DIR_FUNCTIONS . 'database.php');

// make a connection to the database... now
dbconn($strHostName, $strDbName, $strUserName, $strPassword) or die('Unable to connect to database server!');

 

Here is "config.php"

// Define the webserver and path parameters
  define('DIR_APPLICATION', 'app/');
  define('DIR_INCLUDES', 'inc/');
  define('DIR_FUNCTIONS', DIR_APPLICATION . 'functions/');
  define('DIR_CLASSES', DIR_APPLICATION . 'classes/');
  define('DIR_MODULES', DIR_APPLICATION . 'modules/');
  define('DIR_LANGUAGES', DIR_APPLICATION . 'languages/');
  
// Define image directories
  define('DIR_IMAGES', 'images/');
  define('DIR_ICONS', DIR_IMAGES . 'icons/');

 

Here is "database.php"

$strHostName = "localhost"; //e.g., "localhost".
$strDbName = "base_admin"; //Check if yours have prefixes.
$strUserName = "root"; //For the database, not your hosting account.
$strPassword = "";

function dbconn($strHostName, $strDbName, $strUserName, $strPassword) {
}

 

I just keep getting "Unable to connect to database server!"

What am I missing for petes sake.

 

Rab

Link to comment
https://forums.phpfreaks.com/topic/267662-database-connection-issue/
Share on other sites

Your function doesn't do anything. It especially doesn't return a value.

dbconn($strHostName, $strDbName, $strUserName, $strPassword) will always evaluate to false until you add something to your function, particularly a return statement.

change your database.php file to this and see if it works:

 

$strHostName = "localhost"; //e.g., "localhost".
$strDbName = "base_admin"; //Check if yours have prefixes.
$strUserName = "root"; //For the database, not your hosting account.
$strPassword = "";

function dbconn($strHostName, $strDbName, $strUserName, $strPassword) {
mysql_connect($strHostName, $strUserName, $strPassword) or return false;
mysql_select_db($strDbName) or return false;
return true;
}

I don't know why @ nimishprabhu is posted this, but it's absolutely wrong:

function dbconn($strHostName, $strDbName, $strUserName, $strPassword) {
mysql_connect($strHostName, $strUserName, $strPassword) or return false;
mysql_select_db($strDbName) or return false;
return true;
}

 

If, you want return true or false of both functions, you can put all together in an ternary operator.

Example

function dbconn($strHostName, $strDbName, $strUserName, $strPassword) {
return (mysql_connect($strHostName, $strUserName, $strPassword) && mysql_select_db($strDbName)) ? true : false;

 

For sure instead of false , you can return mysql_error() function or anything you want.

 

function dbconn($strHostName, $strDbName, $strUserName, $strPassword) {
return (mysql_connect($strHostName, $strUserName, $strPassword) && mysql_select_db($strDbName)) ? true : mysql_error();

 

var_dump(ddconn());

Just because you CAN use a ternary operator or return the error doesn't mean that returning false is "absolutely wrong"....

He can not return any values in the way he wrote above, that's why I said "is absolutely wrong".

I've never said that return true or return false is an wrong idea. 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.