Rabastan Posted August 27, 2012 Share Posted August 27, 2012 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 More sharing options...
Jessica Posted August 27, 2012 Share Posted August 27, 2012 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 https://forums.phpfreaks.com/topic/267662-database-connection-issue/#findComment-1372989 Share on other sites More sharing options...
nimishprabhu Posted August 27, 2012 Share Posted August 27, 2012 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 https://forums.phpfreaks.com/topic/267662-database-connection-issue/#findComment-1372993 Share on other sites More sharing options...
Rabastan Posted August 27, 2012 Author Share Posted August 27, 2012 OK, I got this now Parse error: syntax error, unexpected 'return' (T_RETURN) in C:\xampp\htdocs\base_site\admin\app\functions\database.php on line 23 Rab Link to comment https://forums.phpfreaks.com/topic/267662-database-connection-issue/#findComment-1372997 Share on other sites More sharing options...
jazzman1 Posted August 27, 2012 Share Posted August 27, 2012 return (mysql_connect($strHostName, $strUserName, $strPassword)) ? true : false; Link to comment https://forums.phpfreaks.com/topic/267662-database-connection-issue/#findComment-1373003 Share on other sites More sharing options...
Rabastan Posted August 27, 2012 Author Share Posted August 27, 2012 Where do I place: return (mysql_connect($strHostName, $strUserName, $strPassword)) ? true : false; in the code Thanx so much Rab Link to comment https://forums.phpfreaks.com/topic/267662-database-connection-issue/#findComment-1373007 Share on other sites More sharing options...
Rabastan Posted August 27, 2012 Author Share Posted August 27, 2012 Thank you all so much!! Rab Link to comment https://forums.phpfreaks.com/topic/267662-database-connection-issue/#findComment-1373010 Share on other sites More sharing options...
jazzman1 Posted August 28, 2012 Share Posted August 28, 2012 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 https://forums.phpfreaks.com/topic/267662-database-connection-issue/#findComment-1373159 Share on other sites More sharing options...
Jessica Posted August 28, 2012 Share Posted August 28, 2012 Just because you CAN use a ternary operator or return the error doesn't mean that returning false is "absolutely wrong".... Link to comment https://forums.phpfreaks.com/topic/267662-database-connection-issue/#findComment-1373181 Share on other sites More sharing options...
jazzman1 Posted August 28, 2012 Share Posted August 28, 2012 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 https://forums.phpfreaks.com/topic/267662-database-connection-issue/#findComment-1373198 Share on other sites More sharing options...
Jessica Posted August 28, 2012 Share Posted August 28, 2012 Ok that makes more sense, sorry. Link to comment https://forums.phpfreaks.com/topic/267662-database-connection-issue/#findComment-1373202 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.