Page 1 of 1

constants help

Posted: Sun Aug 08, 2010 7:38 pm
by njguy
Hello all, I am a new poster so thanks for any help. I am having a problem with figuring out how to reference data in a config file to a database connect class. I know many of you have probably seen this a hundred times, but I am having trouble sifting through all the nonsense on the net and finding a simple example.

Here is database.php:

Code: Select all

<?php

 include("config.php");
 class database {
 
	var $conn;
		
	function __construct()
    {

        $conn = mysql_connect($dbhost, $dbuser, $dbpass);
        $db_selected = mysql_select_db($dbname);

        if (!$conn) {
            die('Could not connect: ' . mysql_error());
        }
        echo 'Connected successfully';
        if (!$db_selected) {
            die ("Can\'t use database : " . mysql_error());
        }else {
            echo 'Database connected sucessfully';
        }
    }

    function insert()
    {
        mysql_query("INSERT INTO users (user_name, first_name)
VALUES ('bob', 'johnson')");
    }
}

?>
Here is config.php:

Code: Select all

define("dbhost", "localhost");
define("dbuser", "");
define("dbpass", "");
define("dbname", "");
So my question is basic, how do I get by database class to pull the info from config? To any volunteers, thanks for taking the time to help a new guy.

Re: constants help

Posted: Sun Aug 08, 2010 8:09 pm
by MindOverBody
Include your config file somewhere on page and replace this line

Code: Select all

$conn = mysql_connect($dbhost, $dbuser, $dbpass);
$db_selected = mysql_select_db($dbname);
with this line

Code: Select all

$conn = mysql_connect(dbhost, dbuser, dbpass);
$db_selected = mysql_select_db(dbname);
Defines are in global scope so including your config file will be enough.
Defines do not need $ sign so it is removed on second code line i provieded.
Hope this helped ;)

ps. One tip, keep your defined variables in uppercase, becouse it is much simplier to discern what is what. for example: define ( "DB_NAME", "database_name" );

Re: constants help

Posted: Mon Aug 09, 2010 5:30 pm
by njguy
I did exactly what you said but I get this error [text]
Warning: mysql_connect() [function.mysql-connect]: Unknown MySQL server host 'dbhost' (11004) in C:\xampp\htdocs\ghost\test.php on line 19

Warning: mysql_select_db() [function.mysql-select-db]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\xampp\htdocs\ghost\test.php on line 20

Warning: mysql_select_db() [function.mysql-select-db]: A link to the server could not be established in C:\xampp\htdocs\ghost\test.php on line 20
Could not connect: Access denied for user 'ODBC'@'localhost' (using password: NO)[/text]

It is not recognizing the define. I put include("config.php"); on the out side of the database class. Don't know why it is not working. :banghead:

Re: constants help

Posted: Mon Aug 09, 2010 7:10 pm
by MindOverBody
Hm, reely duno what is the problem. Check in config files did you put php tags.