SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP connection class help

    I am trying to make the transition from procedural to OO PHP and could use your help. Today I started looking at building a class to connect to any type of database.

    I am sure that I am going wrong in multiple places but my current problem is that after I call set_server and $this->type is set, I follow up by calling query but query isn't picking up the $this->type variable. Where am I going wrong?

    PHP Code:
    <? if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class 
    Connection {
        
        function 
    set_server($type$username$password$hostname$port)
        {
            if(
    $type == "mysql")
            {
                
    $this->link mysql_connect($hostname$username$password);
            }
            
        }

        function 
    query($query_string)
        {
            if (!empty(
    $query_string))
            {
                if(
    $this->type == "mysql")
                {
                    
    $this->query_string $query_string;
                    
    $this->result mysql_query($query_string,$this->link);
                    return 
    $this->result;
                }
            }
            else
            {
                return 
    false;
            }

        }
        
        function 
    fetch($result="")
        {
             if(empty(
    $result))
             { 
                 
    $result $this->result
                 return 
    $result;
             }
             else
             {
                 if(
    $this->type == "mysql")
                 {
                    return 
    mysql_fetch_row($result);
                 }
             }
             
            

        }
        
    }

    ?>

  2. #2
    SitePoint Member
    Join Date
    Feb 2009
    Location
    Here, There, & Everywhere
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by altinvest View Post
    I am trying to make the transition from procedural to OO PHP and could use your help. Today I started looking at building a class to connect to any type of database.

    I am sure that I am going wrong in multiple places but my current problem is that after I call set_server and $this->type is set, I follow up by calling query but query isn't picking up the $this->type variable. Where am I going wrong?

    PHP Code:
    <? if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class 
    Connection {
        
        function 
    set_server($type$username$password$hostname$port)
        {
            if(
    $type == "mysql")
            {
                
    $this->link mysql_connect($hostname$username$password);
            }
            
        }

        function 
    query($query_string)
        {
            if (!empty(
    $query_string))
            {
                if(
    $this->type == "mysql")
                {
                    
    $this->query_string $query_string;
                    
    $this->result mysql_query($query_string,$this->link);
                    return 
    $this->result;
                }
            }
            else
            {
                return 
    false;
            }

        }
        
        function 
    fetch($result="")
        {
             if(empty(
    $result))
             { 
                 
    $result $this->result
                 return 
    $result;
             }
             else
             {
                 if(
    $this->type == "mysql")
                 {
                    return 
    mysql_fetch_row($result);
                 }
             }
             
            

        }
        
    }

    ?>
    It will not pick it up, because type is a parameter to the setServer method. Only the setServer method will know about it.
    You need to create a class member variable to hold the value of type. Then you can access that class variable from other methods. something like:

    Code:
    class Connection
    {
        $thisType='';
    
        function set_server($type, $username, $password, $hostname, $port)
        {
    
            $this->thisType = $type;
    
            if($this->thisType == "mysql")
            {
                $this->link = mysql_connect($hostname, $username, $password);
            }
            
        }
    
        function query($query_string)
        {
            if (!empty($query_string))
            {
                if($this->thisType == "mysql")
                {
                    $this->query_string = $query_string;
                    $this->result = mysql_query($query_string,$this->link);
                    return $this->result;
                }
            }
            else
            {
                return false;
            }
    
        }
        
        function fetch($result="")
        {
             if(empty($result))
             { 
                 $result = $this->result; 
                 return $result;
             }
             else
             {
                 if($this->thisType == "mysql")
                 {
                    return mysql_fetch_row($result);
                 }
             }
             
            
    
        }
        
    }
    
    ?>
    Hope this helps.

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you. That was what I was looking for.


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
  •