TLaude
-
Posts
7 -
Joined
-
Last visited
Never
Posts posted by TLaude
-
-
Ok, that is what I thought. I've been trying that and can't seem to get it to work. I'll play around with it some more. Thanks ignace!
-
class Configuration { const DB_HOST = 'host'; const DB_USERNAME = 'username'; const DB_PASSWORD = 'password'; const DB_NAME = 'name'; } class Database { protected $_host = null; protected $_username = null; protected $_password = null; protected $_name = null; public function __construct() { $this->_host = Configuration::DB_HOST; $this->_username = Configuration::DB_USERNAME; $this->_password = Configuration::DB_PASSWORD; $this->_name = Configuration::DB_NAME; } }
I need them in DIFFERENT files.
-
Here is what I'm trying to accomplish.
I need a file (const_values.php) to store primary values that will allow me to use them all over my website while having the values originate in 1 location so if the value ever changes, I can just change it in the 1 spot.
Then, once const_values.php is set up, how would I go about calling it into my functions.php page?
Here is what I am thinking, but I'm not sure how well it is going to work.
// const_values.php <?php class const_values { function const_values() { define(DB_HOST, '***'); define(DB_USER, '***'); define(DB_PASS, '***'); } } ?>
<?php // functions.php include('const_values.php') var $dbhost; var $dbuser; var $dbpass; $this->dbhost = const_values->DB_HOST; $this->dbuser = const_values->DB_USER; $this->dbpass = const_values->DB_PASS; class database { functions dbConnect() { mysql_connect($this->dbhost, $this->dbuser, $this->dbpass) } } ?>
Am I close or am I far off?
-
Thanks you two. I was hoping I would be able to use a constants.php so I don't have important values like database connection info scattered. I would like to be able to just change it in 1 place (constants.php) and everything will work.
I will use your tips and try to mesh it with mine.
Thanks folks!
-
$this->$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die("Could not connect to server.");
doesn't work because it doesn't know the values $conn, $dbhost, $dbuser, $dbpass
What am I doing wrong that it is not pulling the values of $dbhost, $dbuser, $dbpass from the constants.php file?
$this->$dbselect = mysql_select_db($dbname, $conn) or die("Could not connect to the database.");should be:
$this->$dbselect = mysql_select_db($dbname, $this->$conn) or die("Could not connect to the database.");
but again it doesn't know the value of $dbselect and $this->$conn
Good catch on the $this->$conn. Completely missed it.
So.. How can I go about making sure $conn and $dbselect have a value?
Like this at the class level?:
var $conn; var $dbselect;
-
I'm a VB.NET programmer, but trying to get into PHP to broaden my knowledge. I'm doing a simple registration / login site just to get my hands on it.
The issue I am having is the registration is not completing. I have a feeling it is the way I am connecting to the database but can't seem to figure out where my error is.
Here is what I have.
register.php
$sql = "INSERT INTO users (Username, Password, Email, FullName) VALUES ($username, $password, $email, $name)"; $this->dbConnect('login') or die("Could not connect to server."); $this->dbQuery($sql) or die("Could not query the database."); $this->dbClose();
functions.php:
<?php include('constants.php'); class database { // Each time this class is called, it will connect to the database function __construct() { $this->dbConnect(); } // Creates a connection to the database function dbConnect($dbname) { $this->$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die("Could not connect to server."); $this->$dbselect = mysql_select_db($dbname, $conn) or die("Could not connect to the database."); } // Closes database connection if a connection is currently set function dbClose() { if(isset($this->$conn)) { mysql_close($this->$conn); } } // Query's the databases function dbQuery($sql) { $result = mysql_query($sql); return $result; } function dbNumRows($result) { $rows = mysql_num_rows($result); return $rows; } } ?>
^^ $dbhost, $dbuser, $dbpass all come out of the constants.php file
constants.php:
<?php define($dbhost, 'localhost'); define($dbuser, 'root'); define($dbpass, ''); ?>
Anyone able to point me in the direction of my error?
OOP Constants
in PHP Coding Help
Posted
Whats the difference between this and the following:
I'm assuming the first one will pull in the variables from the class itself?