SOLVED: Help with classes - generating a database connection

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
real_life
Forum Newbie
Posts: 2
Joined: Mon Jan 12, 2004 2:06 pm

SOLVED: Help with classes - generating a database connection

Post 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.
Last edited by real_life on Mon Jan 12, 2004 2:44 pm, edited 2 times in total.
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post 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.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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
real_life
Forum Newbie
Posts: 2
Joined: Mon Jan 12, 2004 2:06 pm

Post 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 ..
}
?>
User avatar
lazy_yogi
Forum Contributor
Posts: 243
Joined: Fri Jan 24, 2003 3:27 am

Post 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
Post Reply