Results 1 to 14 of 14

Thread: Importing an other php file [Part2]

  1. #1

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Importing an other php file [Part2]

    What if I have 2 included files.


    First the index.php file inclused a file (that uses some php) then after that the index file it self uses some php. Then the index file includes an other file that uses PHP.



    Is it then enough to open the database in the first included php file? Or should I open it up before the first included php file in the index file?



    ØØ

  2. #2
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: Importing an other php file [Part2]

    Quote Originally Posted by NoteMe
    What if I have 2 included files.


    First the index.php file inclused a file (that uses some php) then after that the index file it self uses some php. Then the index file includes an other file that uses PHP.



    Is it then enough to open the database in the first included php file? Or should I open it up before the first included php file in the index file?



    ØØ
    I think when it includes a file all it does is takes the code and puts it all together so it kind of builds one script from 3 (in yor case) as to where to put the database opening, I would say put it before the first include then its avalible through the rest of the script.

    Hope that helps

  3. #3
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: Importing an other php file [Part2]

    Hey have you tried using a class? I start out using a method like yours but then switched to using a class so that i wouldnt have to re-write the basics of connecting everytime i started a new project.

    Its not the best code as im still perfecting it at the moment, and im sure there is a better way of doing what im doing
    PHP Code:
     <?php
     
    include_once("error.class.php");
     if(!
    defined("DB.CLASS"))
     {
         
    define("DB.CLASS",true);
         class 
    DBClass
         
    {
             var 
    $server        "sql-server";
             var 
    $database    "database";
             var 
    $username    "username";
             var 
    $password    "password";
             var 
    $prefix        "";
             
             var 
    $DISPLAY_ERRORS false;
             
             var 
    $conn;
             var 
    $ConnectionOpen false;
             var 
    $results;
             
             function 
    connect()
             {
                 
    $this->conn = @mysql_connect($this->server,$this->username,$this->password);
                 if(
    $this->conn != false)
                 {
                     if(@
    mysql_select_db($this->database,$this->conn))
                     {
                         
    $this->ConnectionOpen true;
                         return 
    true;
                     }
                     else
                     {
                         
    $e = new ERROR(mysql_error());
                     }
                 }
                 else
                 {
                     
    $e = new ERROR(mysql_error());
                 }
                 
                 
    $this->ConnectionOpen false;
                 
                 if(
    $this->DISPLAY_ERRORS)
                 {
                     
    //print("ERROR:" . mysql_error());
                     
    $e = new ERROR(mysql_error());
                 }
                 return 
    false;
             }
             
             function 
    close()
             {
                 
    $this->ConnectionOpen false;
                 if(!@
    mysql_close($this->conn))
                 {
                     
    $e = new ERROR(mysql_error());
                 }
             }
             
             function 
    ExecuteSQL($query)
             {
                 if(
    $this->ConnectionOpen)
                 {
                     
    $this->results = @mysql_query($query,$this->conn);
                     if(!
    $this->results)
                     {
                         return 
    true;
                     }
                     else
                     {
                         return 
    false;
                     }
                 }
             }
             function 
    ExecuteGetRow($query)
             {
                 if(
    $this->ConnectionOpen)
                 {
                     
    $results = @mysql_query($query,$this->conn);
                     return @
    mysql_numrows($results);
                 }
             }
             function 
    InsertID()
             {
                 if(
    $this->ConnectionOpen)
                 {
                     return @
    mysql_insert_id();
                 }
             }
             function 
    AffectedRows()
             {
                 if(
    $this->ConnectionOpen)
                 {
                     return @
    mysql_affected_rows($this->conn);
                 }
             }
             function 
    ReturnResults()
             {
                 
    $array = Array();
                 while(
    $row = @mysql_fetch_row($this->results))
                 {
                     
    $temp = Array();
                     for(
    $i 0$i count($row); $i++)
                     {
                         
    $temp[@mysql_field_name($this->results,$i)] = $row[$i];
                     }
                     
    $array[] = $temp;
                 }
                 return 
    $array;
             }
         }
     }
     
    ?>
    PHP Code:
     <?
     $dbc = new DBClass();
     $dbc->Connect();
     if(!$dbc->ConnectionOpen)
     {
     $e = new ERROR(mysql_error()); //or your own error handling
     }
     else
     {
     $dbc->ExecuteSQL($query);
     print_r($dbc->ReturnResults());
     }
     ?>
    Just a thought for the future

  4. #4

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: Importing an other php file [Part2]

    Quote Originally Posted by Pino
    I think when it includes a file all it does is takes the code and puts it all together so it kind of builds one script from 3 (in yor case) as to where to put the database opening, I would say put it before the first include then its avalible through the rest of the script.

    Hope that helps

    OK, so it first copy/asembly the files togheter then running the PHP? Or the other way around? SHould be the other way around if it should be able to import it at all. It is imported using PHP. Unless it first runs PHP to check for inclused. THen copy stuff togheter, and then start from the top again to run the PHP.

    Hehehe...Think I even confused my self now..


    ØØ

  5. #5

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: Importing an other php file [Part2]

    Quote Originally Posted by john tindell
    Hey have you tried using a class? I start out using a method like yours but then switched to using a class so that i wouldnt have to re-write the basics of connecting everytime i started a new project.

    Its not the best code as im still perfecting it at the moment, and im sure there is a better way of doing what im doing
    PHP Code:
     <?php
     
    include_once("error.class.php");
     if(!
    defined("DB.CLASS"))
     {
         
    define("DB.CLASS",true);
         class 
    DBClass
         
    {
             var 
    $server        "sql-server";
             var 
    $database    "database";
             var 
    $username    "username";
             var 
    $password    "password";
             var 
    $prefix        "";
             
             var 
    $DISPLAY_ERRORS false;
             
             var 
    $conn;
             var 
    $ConnectionOpen false;
             var 
    $results;
             
             function 
    connect()
             {
                 
    $this->conn = @mysql_connect($this->server,$this->username,$this->password);
                 if(
    $this->conn != false)
                 {
                     if(@
    mysql_select_db($this->database,$this->conn))
                     {
                         
    $this->ConnectionOpen true;
                         return 
    true;
                     }
                     else
                     {
                         
    $e = new ERROR(mysql_error());
                     }
                 }
                 else
                 {
                     
    $e = new ERROR(mysql_error());
                 }
                 
                 
    $this->ConnectionOpen false;
                 
                 if(
    $this->DISPLAY_ERRORS)
                 {
                     
    //print("ERROR:" . mysql_error());
                     
    $e = new ERROR(mysql_error());
                 }
                 return 
    false;
             }
             
             function 
    close()
             {
                 
    $this->ConnectionOpen false;
                 if(!@
    mysql_close($this->conn))
                 {
                     
    $e = new ERROR(mysql_error());
                 }
             }
             
             function 
    ExecuteSQL($query)
             {
                 if(
    $this->ConnectionOpen)
                 {
                     
    $this->results = @mysql_query($query,$this->conn);
                     if(!
    $this->results)
                     {
                         return 
    true;
                     }
                     else
                     {
                         return 
    false;
                     }
                 }
             }
             function 
    ExecuteGetRow($query)
             {
                 if(
    $this->ConnectionOpen)
                 {
                     
    $results = @mysql_query($query,$this->conn);
                     return @
    mysql_numrows($results);
                 }
             }
             function 
    InsertID()
             {
                 if(
    $this->ConnectionOpen)
                 {
                     return @
    mysql_insert_id();
                 }
             }
             function 
    AffectedRows()
             {
                 if(
    $this->ConnectionOpen)
                 {
                     return @
    mysql_affected_rows($this->conn);
                 }
             }
             function 
    ReturnResults()
             {
                 
    $array = Array();
                 while(
    $row = @mysql_fetch_row($this->results))
                 {
                     
    $temp = Array();
                     for(
    $i 0$i count($row); $i++)
                     {
                         
    $temp[@mysql_field_name($this->results,$i)] = $row[$i];
                     }
                     
    $array[] = $temp;
                 }
                 return 
    $array;
             }
         }
     }
     
    ?>
    PHP Code:
     <?
     $dbc = new DBClass();
     $dbc->Connect();
     if(!$dbc->ConnectionOpen)
     {
     $e = new ERROR(mysql_error()); //or your own error handling
     }
     else
     {
     $dbc->ExecuteSQL($query);
     print_r($dbc->ReturnResults());
     }
     ?>
    Just a thought for the future
    We never looked much into classes in our web class. Didn't have much time for it, and since PHP 5 was around the corner and it was supposed to have much better OO the theacher didn't bother too much about it.


    But your code looks good. Is it PHP 4.x?

  6. #6
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: Importing an other php file [Part2]

    its PHP 4.3 i think, but dont quote me on that. It works for me but it might not work for you just a thought anyway

  7. #7
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Importing an other php file [Part2]

    It won't work if you don't post the Error class too, as the DB object uses it.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  8. #8
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Importing an other php file [Part2]

    Like Pino said, if you include a file, it takes the code from the file and insterts it at the point. Therefore it inherits, as it were, the current scope of the code.

    So if you include a file inside a class function then you will inherit the scope of that function, inlcuding its local variables and the scope of the class. If you include a file inside a function, it will inherit the scope of that function and all its local variables and finally if you include a file globally, you inherit all the global variables.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  9. #9
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: Importing an other php file [Part2]

    Quote Originally Posted by visualAd
    It won't work if you don't post the Error class too, as the DB object uses it.
    woops forgot that
    its only to notify me if something goes wrong. still working on it, like everything else
    PHP Code:
       <?php
       
    if(!defined("ERROR.CLASS"))
       {
           
    define("ERROR.CLASS",true);
           class 
    ERROR extends CONFIG
           
    {
               var 
    $Error_EmailAddress "[email protected]";
               function 
    ERROR($message)
               {
                   
    $msg $message;
                   
    $msg .= "\n\r" $_SERVER['REMOTE_ADDR'];
                   
    $msg .= "\n\r" date("D M j G:i:s T Y",time());
                 
    $headers "From: "$Error_EmailAddress."<" $Error_EmailAddress ">\r \n";
              @
    mail($this->Error_EmailAddress,"ERROR",$msg,$headers);    
                   print 
    "<pre>$msg</pre>";
               }
           }
       }
       
    ?>

  10. #10
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Importing an other php file [Part2]

    What about the CONFIG class, which the ERROR class is derived from?

    Personally I would not use the DB class you have created. The main reason is, is that is is changing the API needed to access the database. One of the beauties of PHP is that all its database functions regardless of the vendor are near identical and return identical results. This means that you can change the database you are using with minimum hassle.

    The idea of wrapping this functionality into an abstraction layer in the form of a class is great, but there are few things that, are not only easy to implement, but will make it a lot more flexible.

    Take a look at how similar the functions are for the MySql sever and MS Sql server API in PHP. This is crying out polymorphism and in theory you cna make several database calsses which all implement the same interface for different database vendors. e.g:
    PHP Code:
    class MySqlDB
    {
    }

    class 
    MSSqlDB
    {
    }

    class 
    PostgreSQLDB
    {

    All these classes need to do is wrap the functions for each database and return exactly the same output. You can then transpraently switch the the vendor of the database without having to change a line of code and have code like this:
    PHP Code:
    switch ($_GET['db'])
    {
        case 
    'MySql':
            
    $db = new MySqlDB;
            break;
        case 
    'PostgreSQL':
            
    $db = new PostgreSQLDB;
            break;
        case 
    'MSSQL':
            
    $db = new MSSqlDB;
            break;
    }

    if (! 
    $result $db->query('SELECCT user_name FROM users')) {
        die(
    $db->error());
    } else {
       while (
    $row $db->fetch_row($result)) { 
          echo(
    $row[0]);
       }

    Its nice to see I'm not the only one who uses classes in PHP though and the same logic can also be applied to your class to allow you to switch the database type easily, I just think it lacks some of the functionality and will mean you will have to go back to the native function calls for this, which defeats the whole purpose.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  11. #11
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: Importing an other php file [Part2]

    yer your right, I think ill either end up completely changing the whole thing a million times, or just scrapping it and becomming a hobo

    What do you think about PHP5 having Public and Private for the classes?

  12. #12
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Importing an other php file [Part2]

    Quote Originally Posted by john tindell
    What do you think about PHP5 having Public and Private for the classes?
    PHP 5 OOP is 100% better then PHP4. Its now fully OO whereas PHP 4 misses the encapsulation side of things. It also allows you to create interfaces and abstract classes which means your code is less prone to errors.

    The main thing is that objects are now by defualt treated as references. So when you assign an object ot a variable, it actually assigns that object and not a copy of it.

    Your class would be easy to change. If you are worried about it breaking other code which depends on it then just keep the orinigal interface and add to it.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  13. #13
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: Importing an other php file [Part2]

    Quote Originally Posted by visualAd
    Your class would be easy to change. If you are worried about it breaking other code which depends on it then just keep the orinigal interface and add to it.
    That was one of the main reasons i used a class so i could change it completely when i discover a better way.

    Quote Originally Posted by visualAd
    PHP 5 OOP is 100% better then PHP4. Its now fully OO whereas PHP 4 misses the encapsulation side of things. It also allows you to create interfaces and abstract classes which means your code is less prone to errors.

    The main thing is that objects are now by defualt treated as references. So when you assign an object ot a variable, it actually assigns that object and not a copy of it.
    I havent really looked into PHP5 that much, i was speak to one of the tutors at my college and he was saying bad stuff about PHP5 being too OO so i didnt really look into it anymore.

    So does PHP5 look like Java or C# now?

  14. #14
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Importing an other php file [Part2]

    PHP5 can still be completely un-OOP if that's what you desire, so your tutor's rant was unjustified. But PHP5 supports proper OOP, unlike PHP4.

    So yes, it's much more like Java, but not stricly typed.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

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



Click Here to Expand Forum to Full Width