SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    My precious!!! astericks's Avatar
    Join Date
    Mar 2002
    Location
    Vancouver, BC
    Posts
    1,971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Weird error - database class

    Hey guys, I'm getting this weird error with my database class.

    I'm getting an error on line 6 (the line between the [.B] tags in my php codes var $mydb...) :
    unexpected T_VARIABLE in mysql.php on line 6

    global.php has the following variables:
    $dbname = "haha";
    $dbuser ="hihi"; // etc etc
    PHP Code:
    @include('global.php'); 
    class 
    db {

    [
    B]    var $mydb = array ('sql_database'   => $dbname,[/B]
                           
    'sql_user'       => $dbuser,
                           
    'sql_pass'       => $dbpass,
                           
    'sql_host'       => "localhost",
                           
    'sql_port'       => ""         ,
                           
    'persistent'     => "0"
                         
    );
    // etc etc

    The parser is choking on the $ sign. If I get rid of the $ sign on line 6, the error moves to line 7. If I hardcode the mysql info [user, pass, db etc], the script works fine.

    Can anyone cast some light on that plz?

    Thanks

  2. #2
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    Germany
    Posts
    550
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you could use something like this instead:

    PHP Code:
    class db {
      var 
    $mydb;

       function 
    db()
         {
             global 
    $dbname;
             global 
    $dbuser;
             global ...;
              
    $this->mydb = array('sql_database' => $dbname, ...);
         }

    or pass them in the constructor:

    PHP Code:
    class db {
       var 
    $mydb;

       function 
    db($dbname$dbuser, ...)
       {
           
    $this->mydb = array('sql_database' => $dbname, ...);
       } 
    This would make the class less "static" i think and you could use it for connecting to more than one database.

    My guess to your error is that you're not allowed to have variable declarations when declaring your class variables (what a sentence )

  3. #3
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    class db {
    var 
    $mydb;

    function 
    db($dbuser$dbuser$dbpass)
    {
        
    $this->mydb = array ('sql_database'   => $dbname,
                           
    'sql_user'       => $dbuser,
                           
    'sql_pass'       => $dbpass,
                           
    'sql_host'       => "localhost",
                           
    'sql_port'       => ""         ,
                           
    'persistent'     => "0"
                         
    );
    }
    // etc etc


  4. #4
    My precious!!! astericks's Avatar
    Join Date
    Mar 2002
    Location
    Vancouver, BC
    Posts
    1,971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the suggestion guys, but I already know about those.

    I just thought there might be a special thing wrong with my codes above, apparently not...ah well, I decided to use globals for that one function

    cheers


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •