constants help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
njguy
Forum Newbie
Posts: 2
Joined: Sun Aug 08, 2010 7:27 pm

constants help

Post 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.
User avatar
MindOverBody
Forum Commoner
Posts: 96
Joined: Fri Aug 06, 2010 9:01 pm
Location: Osijek, Croatia

Re: constants help

Post 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" );
njguy
Forum Newbie
Posts: 2
Joined: Sun Aug 08, 2010 7:27 pm

Re: constants help

Post 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:
User avatar
MindOverBody
Forum Commoner
Posts: 96
Joined: Fri Aug 06, 2010 9:01 pm
Location: Osijek, Croatia

Re: constants help

Post by MindOverBody »

Hm, reely duno what is the problem. Check in config files did you put php tags.
Post Reply