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
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.

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

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());

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.