|
Posted by Jerry Stuckle on 12/06/62 11:57
Slant wrote:
> Fantastic reply! Thanks for taking the time. I shall now attempt to
> update my situation in order to hopefully clear up any confusion.
>
> I figured out how to get the Stuff class to grab data from the Database
> class. Here is an updated version of my code:
>
>
> class Database {
> function __construct() {
> // Save the database credentials
> $db['host'] = "localhost";
> $db['user'] = "root";
> $db['pass'] = "...";
> $db['name'] = "tbl";
> // Initialize the database connection
> $link = mysql_connect($db['host'],$db['user'],$db['pass']);
> mysql_select_db($db['name'],$link);
> }
> function getLinks() {
> $query = "SELECT * FROM links";
> $result = mysql_query($query) or die(mysql_error());
> while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
> $this->links[$row['id']]['label'] = $row['label'];
> $this->links[$row['id']]['url'] = $row['url'];
> $this->links[$row['id']]['location'] = $row['location'];
> }
> }
> }
>
> class Link {
> function __construct() {
> $this->database = new Database;
> }
> function getLinks() {
> $this->database->getLinks();
> $this->links = $this->database->links;
> }
> }
>
>
> This is exactly what I'm using now, in fact. I've done away with the
> arbitrary naming from before. So, the Link class instantiates a
> Database object, then passes a request to it. The database object then
> returns a variable which is then accessed by the index.php file and
> displayed there in a foreach statement.
>
> Your questions:
>
> Why use a global? I'm not really even sure how to NOT use a global.
> I'm assuming you're referring to the scope, right? This actually is
> why I'm still on this thread. My question today ties directly into
> this: How can I define some constants in a config.php file or something
> of the like, then access those constants from within the Database class
> directly without HAVING to pass the variables every time I instantiate
> a Database object? As you can see now, I am actually declairing the
> variables INSIDE of the database class... not my cup of tea.
>
> What does the Stuff class do?
> It's just a go between so the actually layout can access the database
> through another layer. It makes sense in the grander scheme of my
> application, just not here. I know it look unneccessary.
>
> My question:
>
> Was just asked in the "Why use a global?" answer.
>
> Thanks as always for your help!!
>
PMJI,
Generally I pass the values to the database class. The main reason is
for security; a second is versatility.
For instance - I might have one user for almost everyone who can read
the database but not update it (or at least certain tables) - or at
least only insert/update. But an admin user would be able to perform
additional operations.
That way the "public" user would have limited privileges, while admin
would have full privileges. It helps in case for some reason the
password gets out (i.e. screwed up Apache configuration) and/or someone
tries a SQL injection attack that I didn't catch.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Navigation:
[Reply to this message]
|