0

So I am working on PHP and hence I got to know about __call() method since, it calls the unknown method and so here I call the method name as it's the table name of my database and as I want to access all the serial number(sn) from the database and print on screen using this method. I know there is very big mistake in my code and I'm not getting it, I'm struck. Please HELP! and if you are not getting my point then please ask, I'll explain!

WHAT I ACTUALLY WANT FROM THIS SCRIPT is that I want a access from database using __call() method and var_dump() the data from the database like I am doing here,

public function getOne($table_name) {
        $rss= mysqli_query(self::$connection,"SELECT * FROM $table_name LIMIT 1");
        return mysqli_fetch_assoc($rss);
    } 

here I am directly getting all the data from database and it echo on the screen, but with __call() also I want to do the same but with rows. How can I do that? Also what is the actual use of __call()?

class mysql_db {
    private static $instance;
    public static $connection;

    public function __construct($host=null, $user=null, $password=null, $db=null) {
        if(!self::$instance && !$host) {
            throw new \Exception("Missing Database..!!!");
        }
        self::$connection = mysqli_connect($host,$user,$password,$db);
    }

    public function __destruct() {
        mysqli_close(self::$connection);
    }       

    public function getData($table_name) {
        $rs= mysqli_query(self::$connection,"SELECT * FROM $table_name");
        return mysqli_fetch_all($rs,MYSQLI_ASSOC);
    }

    public function getOne($table_name) {
        $rss= mysqli_query(self::$connection,"SELECT * FROM $table_name LIMIT 1");
        return mysqli_fetch_assoc($rss);
    }

    public function find($table_name,$sn,$val) {
        $rq= mysqli_query(self::$connection,"SELECT * FROM $table_name WHERE $sn='$val'");
        return mysqli_fetch_all($rq,MYSQLI_ASSOC);
    }


**//   I AM GETTING ERROR FROM THESE LINES**

//->    public function __call($table_name,$sn) {
//->        //var_dump("SELECT * FROM $table_name where sn='1'");
//->        $rt= mysqli_query(self::$connection,"SELECT * FROM $table_name 
            WHERE '$sn'")or die(mysqli_error()) ;
//->        return mysqli_fetch_all($rt,MYSQLI_ASSOC);
    }

    public static function getInstance() {
        try {
            if(!self::$instance) {
                self::$instance=new static('localhost','root','','xam');
            }
        } catch(\Exception $e) {
            echo 'Caught Error: ', $e->getMessage(), "\n";
        }
        return self::$instance;
    }
}
$h= mysql_db::getInstance();
var_dump($h->name("1"));

1 Answers1

1

The __call() method is one of php magic methods.

As it states in the documentation:

The overloading methods are invoked when interacting with properties or methods that have not been declared or are not visible in the current scope

About the __call() magic method:

__call( string $name, array $arguments) is triggered when invoking inaccessible methods in an object context.

Why is your code not working?

The error message states that you are trying to convert an array into a string. So probably, the variable $sn that you're passing to the __call() method is not an array. Also, you're not concatenating your strings properly. If you want to inject a variable inside a string. The correct term is string interpolation, and it should be implemented as it follows:

echo "Variables should always be inside brackets. My {$name} is!"

Also, remember to sanitize your querys to avoid sql injections! check this answer

taavs
  • 509
  • 7
  • 11