Problem with static function escape_string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
duncanwilkie
Forum Newbie
Posts: 17
Joined: Wed Feb 28, 2007 3:26 pm
Location: Bristol, UK

Problem with static function escape_string

Post by duncanwilkie »

Hi All,

I'm having trouble using a class's static method, with mysqli_real_escape_string - I'm getting an error:
Fatal error: Using $this when not in object context in C:\Users\Duncan\Websites\...\logic\clsDb.php on line 42

I'm trying to build an Interface, Class implementing it and get it working (all in one php file, called clsDB.php).
The issue I'm having is with returning the myslqi_real_escape_string value.

Line 42 (red highlight): Class code in method generating the error message.
Line 87 (blue highlight): Class method call for db usage example.

Please let me know if you see any other issues with this code:

Code: Select all

 
<?php
// clsDb.php
 
interface Db{
    public function connect();
    public function error();
    public function errno();
    public static function escape_string($string);
    public function query($query);
    public function fetch_array($result);
    public function fetch_row($result);
    public function fetch_assoc($result);
    public function fetch_object($result);
    public function num_rows($result);
    public function close();
}
 
 
class mySqlDb implements Db{
    private $link;
    
    public function connect($server='', $username='', $password='', $new_link=false, $client_flags=0){
        $this->link = new mysqli($server, $username, $password, $new_link, $client_flags);
    }
    
    //Logs in using the standard connection for exampleuserdb - root user.
    public function connectStandard(){
        $this->link = new mysqli('localhost', 'root', '', 'exampleuserdb', 0);
    }
    
    public function errno(){
        return $this->link->errno();
    }
    
    public function error(){
        return $this->link->error();
    }
    
    public static function escape_string($string){
        
        [color=#FF0000]return $string = $this->link->real_escape_string($string);[/color]
        //$string = $this->link->real_escape_string($string);
        //return $string;
        //return mysqli_real_escape_string($link, $string);
        //return mysqli_real_escape_string($link, $string); 
    }
    
    public function query($query){
        return $this->link->query($query);
    }
    
    public function fetch_array($result, $array_type = MYSQL_BOTH){
        return $result->fetch_array($array_type);
    }
    
    public function fetch_row($result){
        return $result->fetch_row();
    }
    
    public function fetch_assoc($result){
        return $result->fetch_assoc();
    }
    
    public function fetch_object($result){
        return $result->fetch_object();
    }
    
    public function num_rows($result){
        return $result->num_rows;
    }
    
    public function close(){
        return $this->link->close();
    }
}
 
 
//Usage example
$db = new mySqlDb;
 
    //If you want to specify a different, username, pass or database use 'connect'
    //$db->connect('localhost', 'root', ''); 
    $db->connectStandard();
    
    $fred = 'Freds';
    [color=#0000FF]$fred = $db->escape_string($fred);[/color]
    echo $fred;
    
    $db->query('use exampleuserdb');
    
    $result = $db->query("SELECT username from users");
        
    while($row = $db->fetch_assoc($result)){
        echo($row['username']);
    }
    
    
        
    $db->close();
    
?>
 
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Problem with static function escape_string

Post by jackpf »

You can't use $this with static functions. You have to access the method using $object->function(), or use "self" or "static". But you won't have access to non-static properties.
duncanwilkie
Forum Newbie
Posts: 17
Joined: Wed Feb 28, 2007 3:26 pm
Location: Bristol, UK

Re: Problem with static function escape_string

Post by duncanwilkie »

Sorry - I'm just not getting it.

I've taken off static for now, and I'm getting the right results.

I took the 'static function' from a guide. I'm not really sure, what it does, and why I should use it.
Which is probably why I don't understand the issue.

Can you provide an example of how to fix?
Thanks.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Problem with static function escape_string

Post by jackpf »

Ok, check this out:

Code: Select all

<?php
class foo
{
    private
        $bar = 'this should work';
    private static
        $baz = 'this should also work';
    
    public function bar()
    {
        echo $this->bar; // will work
    }
    public static function static_bar()
    {
        echo $this->bar; // won't work
    }
    public static function baz()
    {
        echo self::$baz; // will work
    }
}
 
$foo = new foo;
$foo->bar();
foo::static_bar(); // fatal error
foo::baz();
?>
Post Reply