News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

Confusion on Scripting

Started by Ardenn, September 21, 2004, 11:54:36 AM

Previous topic - Next topic

Ardenn

So im reading an article over at PHP Builder about how to build a CMS, and basically, this guy is building his own CMS and writing articles about each step in his process.  Well Im trying to follow along, but I am confused about some code he is using and wanted to ask you guys if you might help me better understand it.

So here is the code:

<?php
////////////////////////////////////////////////////////////////////////////////////////
// Class: DbConnector
// Purpose: Connect to a database, MySQL version
///////////////////////////////////////////////////////////////////////////////////////
require_once 'SystemComponent.php';

class 
DbConnector extends SystemComponent {

var 
$theQuery;
var 
$link;

//*** Function: DbConnector, Purpose: Connect to the database ***
function DbConnector(){

// Load settings from parent class
$settings SystemComponent::getSettings();

// Get the main settings from the array we just loaded
$host $settings['dbhost'];
$db $settings['dbname'];
$user $settings['dbusername'];
$pass $settings['dbpassword'];

// Connect to the database
$this->link mysql_connect($host$user$pass);
mysql_select_db($db);
register_shutdown_function(array(&$this'close'));

}

//*** Function: query, Purpose: Execute a database query ***
function query($query) {

$this->theQuery $query;
return 
mysql_query($query$this->link);

}

//*** Function: fetchArray, Purpose: Get array of query results ***
function fetchArray($result) {

return 
mysql_fetch_array($result);

}

//*** Function: close, Purpose: Close the connection ***
function close() {

mysql_close($this->link);

}


}
?>


Now let me break the code down, as far as I understand it.  First this code opens and reads the file 'SystemComponent.php' which basically contains a function that defines an array called settings:

Here is the function:

function getSettings() {

// System variables
$settings['siteDir'] = '/path/to/your/intranet/';

// Database variables
$settings['dbhost'] = 'hostname';
$settings['dbusername'] = 'dbuser';
$settings['dbpassword'] = 'dbpass';
$settings['dbname'] = 'mydb';

return 
$settings;

}


Now once this is included, the script says that the DBConnector class is a child class of SystemComponent and it defines these two variables $theQueryand $Link.  Next the script defines the settings array as the same array used in the function getSettings() and passes those variables to the standard mySQL connection variable names.  So far no mystery. 

The part that confuses me is almost everthing below the // Connect to the database comment. Ok so here goes:

$this->link = mysql_connect($host, $user, $pass);

Is he saying that $this is an array with the element [link] and that element equals the mysql_connect instruction?

mysql_select_db($db);
I understand this

register_shutdown_function(array(&$this, 'close'));

After reading the PHP manual on this function, Im still not sure what this line of code is doing.

function query($query) { 
Understand this fine

$this->theQuery = $query;
So is this saying that the array $this with the element theQuery is equal to a variable named $query?

return mysql_query($query, $this->link);

Im guessing on this because I've never seen this type of usage:the function will return a mysql query with $query as an argument (presumably defined later) and the array $this with element link, which is equal to the hostname, username, and password.  So if I where to guess what this might look like would it be something like:


mysql_query("SELECT* FROM TABLENAME WHERE 1=1", "USERNAME","HOSTNAME","PASSWORD")


Im not sure...

I understand the function  fetchArray($result)

I dont understand

mysql_close($this->link);

So this is equilalent to:

mysql_close("USERNAME","HOSTNAME","PASSWORD")


???

I know there is alot here, but just want to understand what this guy is doing.


Ardenn // Traxxus
http://www.twinwand.com
D&D Play by Post Community Looking for Players and DM's

roboter88

#1
Quote
Code:
mysql_close($this->link);

So this is equilalent to:

Code:
mysql_close("USERNAME","HOSTNAME","PASSWORD")
Yes and its a security risc when u dont close the database connecton after you recive or send a query to the database. And normaly a mysql_close() is enough.
This script function connects to the database and to do this its need the database login (user/psw) and the location (host) and the database name wich you want to access.

Now the functions eg. query
Are used to interact with the database u just established the connection to.
You sent QUERYS to in MySQL language to get or send information eg. read some array with infos eg. results.
This code you posted is normaly the function.php (including all your project functions).
You can use them form everywhere (when included) so you just need to write em down once.

Ok im a php NOOb too so plz wait for further assistance :) I just try to learn it also, hehe.


[Unknown]

Yuck, OOP.  The $this variable means "this class".  And, $this->var is referring to:

class something
{
   var $var;
}

That $var.  It means, not the local one ($var) but the one for this class.

Yuck OOP :P.

-[Unknown]

Ardenn

OHHHHH.. so the $this isnt an array, but its telling the script to use the variable defined IN the class rather than an identical variable that may be used outside the class in the regular script?
Ardenn // Traxxus
http://www.twinwand.com
D&D Play by Post Community Looking for Players and DM's

[Unknown]

Quote from: Ardenn on September 21, 2004, 05:31:03 PM
OHHHHH.. so the $this isnt an array, but its telling the script to use the variable defined IN the class rather than an identical variable that may be used outside the class in the regular script?

Well, basically... not *exactly*, but you've got the idea.  Here's a better example:

<?php

global $a_global_variable;

$a_global_variable 'hiya';

class 
some_class
{
   var 
$a_class_variable 'caviar';

   function 
some_func()
   {
      
$a_local_variable 'chocolate';

      echo 
$a_global_variable// <-- error, not defined.
      
echo $a_class_variable// <-- error, not defined.
      
echo $a_local_variable// <-- chocolate ;).

      
global $a_global_variable;

      echo 
$a_global_variable// <-- hiya!
      
echo $GLOBALS['a_global_variable']; // <-- hiya!

      
echo $this->a_class_variable// <-- caviar.
   
}
}

?>


These are different "scopes".  See, the local variable was defined in the local scope... by default, you can only access the local scope.  By using global, you can "extract" things from the global scope "into" the local scope.

The class "scope" is a bit different.  Here, $this is actually a variable.  It's in the local scope by default ;).  If you want the class variables, you use an ->.  This means "this class's" variable.

-[Unknown]

Advertisement: