php OOP help please

Discussion in 'PHP' started by JEET, Oct 10, 2009.

  1. #1
    Hi,
    I got this class:
    
    
    <?php
    class db{
    public $conn;
    public $dbs;
    
    function connect( $host, $user, $pass, $name ){
    $this->conn= mysql_connect($host, $user, $pass) or die(mysql_error());
    $this->dbs= @mysql_select_db($name) or die(mysql_error());
    }
    
    function exec($query){
    if($this->conn){
    $q= mysql_query($query) or die(mysql_error());
    return $q;
    }
    }
    
    }//class end
    ?>
    
    Code (markup):
    Then I do the following:

    
    $db= new db;
    $db->connect($host, $user, $pass, $database_name);
    $s= $db->exec(" some query");
    
    Code (markup):
    and I get this error:

    Fatal error: Using $this when not in object context in /path/db.php on line 21

    Line 21 is:
    if($this->conn){

    What am I doing wrong here?
    I want to check if a connection is made to the database or not before sending the query. If I cannot do it like this, how should I do it?

    Thanks :)
     
    JEET, Oct 10, 2009 IP
  2. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #2
    I don't see any problem in your code, the only thing I can see, you using the reserve function name exec in your function decrelation.

    try rename that function to other name and try again.

    Change the following function name to other name
    function exec(){

    }
     
    php-lover, Oct 10, 2009 IP
  3. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #3
    Hi,
    I changed it to: run_this() but I still get that error.
    Thanks
     
    JEET, Oct 10, 2009 IP
  4. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #4
    function connect( $host, $user, $pass, $name ){
    $this->conn= mysql_connect($host, $user, $pass) or die(mysql_error());
    $this->dbs= @mysql_select_db($name) or die(mysql_error());
    }

    Double check your connect function to make sure it's connect, check your database name as well.
     
    php-lover, Oct 10, 2009 IP
  5. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #5
    ini_set('display_errors', 1);
    class db{
    public $conn;
    public $dbs;
    
       function connect( $host, $user, $pass, $name ){
       $this->conn= mysql_connect($host, $user, $pass) or die(mysql_error());
       $this->dbs= mysql_select_db($name) or die(mysql_error());
       }
       
       function exec($query){
          if($this->conn){
          $q= mysql_query($query) or die(mysql_error());
          return $q;
          }
       }
    
    }//class end
    PHP:
    Remove the @ from mysql_select_db
    and test your code.

    The main reason I think the error occur because there's an error while building the object db in your connect function, and your code hang with incomplete object.
    The error might cause by invalid login or database name.
     
    php-lover, Oct 10, 2009 IP
  6. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #6
    Hi,
    Removed the @ sign, but still no use. If I remove this line:
    if($this->conn){

    then the entire script works properly, executing the mysql query and getting output from database.
    But as soon as I put that line back, the same script gives a fatal error.
    The database connection is made otherwise the script won't work without that line as well.
    Thanks
     
    JEET, Oct 10, 2009 IP
  7. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #7
    Hi,
    Found the error. In case someone else makes this mistake, here's what happenned--

    There's a seperate function in another class which is making a call to this function, and it's doing it like this:
    $s= db::run_this(" some query ");
    and not like:
    $s= $db->run_this(" some query ");

    This is why I'm not able to use $this->conn when calling the function like: db::run_this()
    Changed it to: if($conn){
    and it works now.

    Is there some other way to access $conn from another class? $conn is a variable in "class db", but if I want to access it in "class something_else", how do I do it?
    Thanks :)
     
    JEET, Oct 10, 2009 IP
  8. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #8
    $db = new db;
    $page = new Page;
    
    class Page{
    
      public function __construct(){
        global $db;
        $query = 'SELECT * FROM JEET';
        $result = $db->exec($query);
    
        while($j = mysql_fetch_array($result)){
             echo $j['bike'];
             echo $j['car'];
             echo $j['tshirt'];
             echo $j['cake'];
        }    
    
     }
    
    }
    PHP:
     
    Last edited: Oct 10, 2009
    php-lover, Oct 10, 2009 IP
    JEET likes this.