类中的mysql查询方法返回错误的值(布尔类型而不是集合)

类中的mysql查询方法返回错误的值(布尔类型而不是集合)

问题描述:

i wrote a class to connect to the database and execute some queries. here is my class code:

class Db_connector extends System_init{
    var $link;
    function Db_connector(){
        $settings=System_init::getSettings();
        $db_host=$settings['db_host'];
        $db_name=$settings['db_name'];
        $db_uname=$settings['db_username'];
        $db_password=$settings['db_password'];

        $this->link=new PDO("mysql:host=$db_host; dbname=$db_name", $db_uname, $db_password);
        $this->link->exec("SET CHARACTER SET utf8");
        $this->link->exec("SET NAMES 'utf8'");
        register_shutdown_function(array(&$this,'close'));
    }

    function query($query){
        $result=$this->link->prepare($query);
        return $result->execute();
    }
}

when i use an object of the above class and use query method of that, the method returns a boolean value that have '1' value. the query that i pass to the method is absolutely correct.

how can i solve this? thanks...

To start with you are getting the correct value. The execute method returns a boolean. And true can evaluate to 1. So it is totally correct.

$stmt = $this->link->prepare($query);
$stmt->execute();
return $stmt->fetchAll();

That is what you probably want.