Page 1 of 1

SOLVED: Help with classes - generating a database connection

Posted: Mon Jan 12, 2004 2:06 pm
by real_life
Hi all,

i'm new to PHP, so I might look silly here :oops:

Here's the question, I would like to instantiate a Database class, which creates a connection to a MySQL database, and nothing else (just trying things out)

Code: Select all

<?php

include("constants.inc");
include("Database.class.php");
$Database = new Database($DB_HOST , $USERNAME , $PW , $DBNAME);

?>
constants.inc contains all the required variables $DB_HOST, $USERNAME etc.

This is database.class.php:

Code: Select all

<?php 

class Database 
{ 
   var $DatabaseHost; 
   var $DatabaseUser; 
   var $DatabasePass; 
   var $DatabaseName; 

   function Database($1,$2,$3,$4) 
   { 
      $this->DatabaseHost = $1; 
      $this->DatabaseUser = $2; 
      $this->DatabasePass = $3; 
      $this->DatabaseName = $4;
      $ServerConnection = mysql_pconnect( $this->DatabaseHost , $this->DatabaseUser , $this->DatabasePass 

); 
      if ( !$ServerConnection ) 
      { 
         echo( "Sorry, but a connection to the server could not be made " . mysql_error() ); 
      } 
      
      $DatabaseConnection = mysql_select_db( $this->DatabaseName , $ServerConnection ); 

      if ( !$DatabaseConnection ) 
      { 
         echo( "Sorry, but a connection to the database could not be made " . mysql_error() ); 
      }
   } 

}

?>
I get a Parse error: parse error, expecting `')'' on the line

Code: Select all

function Database($1,$2,$3,$4)
I really don't understand why it's not working, it all makes sense to me. Did i miss anything?

Thanks in advance.

Posted: Mon Jan 12, 2004 2:41 pm
by DuFF
From PHP.net:
Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'

Note: For our purposes here, a letter is a-z, A-Z, and the ASCII characters from 127 through 255 (0x7f-0xff).

Code: Select all

<?php
$var = "Bob";
$Var = "Joe";
echo "$var, $Var";      // outputs "Bob, Joe"

$4site = 'not yet';    // invalid; starts with a number
$_4site = 'not yet';    // valid; starts with an underscore
$täyte = 'mansikka';    // valid; 'ä' is (Extended) ASCII 228.
?>
PHP doesn't allow variables to start with a number. It is generally good practice to have descriptive names of your variables anyway, the variable $1 doesn't really tell you anything.

Posted: Mon Jan 12, 2004 2:41 pm
by McGruff
Doesn't answer your question directly but you might be interested to see how Eclipse does it (see the MyDatabase.php file).

http://www.students.cs.uu.nl/people/voo ... /index.php

Posted: Mon Jan 12, 2004 2:43 pm
by real_life
thanks for the answers! A quick modification on the variable names in the constructor did it.

Code: Select all

<?php
function Database($param1, $param2, $param3, $param4)
{
      $this->DatabaseHost = $param1; 
      $this->DatabaseUser = $param2; 
      $this->DatabasePass = $param3; 
      $this->DatabaseName = $param4;
   // truncated ..
}
?>

Posted: Mon Jan 12, 2004 2:57 pm
by lazy_yogi
Also, you might like to make a config class like in the first post here which has the parameters. And have your db class inherit from it.

That way you don't need to include the .inc file every time you include the db class.
http://www.devnetwork.net/forums/viewto ... 83&start=0