including a php file in php class?

Discussion in 'PHP' started by fiza23, May 24, 2009.

  1. #1
    i have a php class named as dbhandler and i want to include the php file dbcon in that class how can i do that plz help
    dbcon.php
    <?php
    $dbhost ="localhost";
    $dbuser = "root";
    $dbpass = "admin1234";
    $dbname = "ue";
    ?>

    dbhandler.php

    <?php
    ////////class defination////////////
    class dbhandler
    {
    //include "dbcon.php";
    public function connectdb()
    {
    //$connection=mysql_connect($dbhost,$dbuser,$dbpass) or print "not connected";
    // mysql_select_db($dbname) or print "no DB";
    $connection=mysql_connect("localhost","root" ,"admin1234") or print "not connected";
    mysql_select_db("ue") or print "no DB";

    }
    ///////function for closing the connection/////////
    public function disconnectdb()
    {
    mysql_close($connection);
    }
    ////////////function for adding the record to db ////////////
    public function addrecord($qry)
    {
    $results=mysql_query($qry) or
    die(mysql_error());
    return true;
    }
    ////////////function for selecting the record from db ////////////
    public function selectrecord($qry)
    {
    $results=mysql_query($qry) or
    die(mysql_error());
    return $results;
    }
    ////////////function for deleting the record from db ////////////
    public function deleterecord($qry)
    {
    $results=mysql_query($qry) or
    die(mysql_error());
    return true;
    }
    /////////////function for updating record//////////////
    public function updaterecord($qry)
    {
    $results=mysql_query($qry) or
    die(mysql_error());
    return true;
    }

    }
    ?>
     
    fiza23, May 24, 2009 IP
  2. Vbot

    Vbot Peon

    Messages:
    107
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Put it in the function connectdb() like this.
    
    public function connectdb()
    {
    include "dbcon.php";
    
    PHP:
     
    Vbot, May 25, 2009 IP
  3. Chikey.ru

    Chikey.ru Peon

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    vbot, i think it is be more simple

    include "dbcon.php";

    class dbhandler
    { //define data as global variable
    global $dbhost;
    global $dbuser;
    ......

    public function connectdb()
    {


    and this variables will be can using along all script
     
    Chikey.ru, May 25, 2009 IP
  4. Chikey.ru

    Chikey.ru Peon

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    ps/ your class bad for using.

    use simple function

    function q ($string) {
    return mysql_query($string) or $debugger .= (mysql_error());
    }

    for debugging not using 'die'
     
    Chikey.ru, May 25, 2009 IP
  5. felluahill

    felluahill Peon

    Messages:
    31
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    It's better to use the define() function to define the details as globals. Once you do that you can just put:

    require_once("dbcon.php");
    Code (markup):
    At the top of the code and then call the globals from within the class.
     
    felluahill, May 25, 2009 IP
  6. Chikey.ru

    Chikey.ru Peon

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    of course, its best solution)
     
    Chikey.ru, May 25, 2009 IP
  7. catgnome

    catgnome Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Actually, if you want to deal with classes, use __construct() ! Also, instead of using simple variables ( database information ), use define() ;)

    function __construct() {
         require_once('dbcon.php');
         // assign your config file values to class variables, etc.
    }
    PHP:
     
    catgnome, May 25, 2009 IP