drxyaox153896 2015-11-16 01:31
浏览 64
已采纳

类中的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...

  • 写回答

1条回答 默认 最新

  • duanhuang1699 2015-11-18 18:02
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效