当前位置:Gxlcms > PHP教程 > 小白求助:php数据库出错,Fatalerror:Calltoamemberfunctionexec()onanon-objectin

小白求助:php数据库出错,Fatalerror:Calltoamemberfunctionexec()onanon-objectin

时间:2021-07-01 10:21:17 帮助过:25人阅读

今天整理了下以前的pdo数据库封装类,但是在使用的时候报错,Fatal error: Call to a member function exec() on a non-object in sqlcontrol.class.php on line 45 这个是完整的错误提示,在以前没有修改的时候没有这个错误,这是怎么回事儿。
这个是我用的类

header("content-type:text/html;charset=utf-8");
classdbPdoManger
{private$conn='';//连接数据库服务器的资源类型private$host="";//主机地址private$dbname="";//数据库名称private$user="";//数据库用户名private$pwd="";//密码private$charset="";//链接编码private$config=array();

    /*
     * 构造函数初始化数据库
     * 变量: $host连接的服务器名称
     *     $user登陆服务器的用户名
     *     $pwd登陆服务器的密码
     */publicfunction__construct($config)
    {$this->config=$config;
        $this->host=$this->config["host"];
        $this->dbname=$this->config["dbname"];
        $this->user=$this->config["user"];
        $this->pwd=$this->config["pwd"];
        $this->charset=$this->config["charset"];
        //$this->open();
    }

    /*
     * 打开数据库
     */publicfunctionopen()
    {$this->conn=new PDO("mysql:host=".$this->host.";dbname=".$this->dbname,$this->user,$this-pwd);
        $this->conn->query('set names '.$this->charset);
        echo$this->conn;
    }

    /*
     * 增删改
     */publicfunctionexecSql($sql)
    {$bool=$this->conn->exec($sql);
        if($bool>0)
        {
            returntrue;
        }else
        {
            returnfalse;
        }
    }

    /*
     * 查询一条数据
    */publicfunctionquer($sql,$mode=PDO::FETCH_ASSOC)
    {$result=$this->conn->query($sql);
        $result->setFetchMode($mode);
        $re=$result->fetch();
        $result=null;
        return$re;
    }

    /*
     * 查询多条数据
    */publicfunctionquerMore($sql,$mode=PDO::FETCH_ASSOC)
    {$result=$this->conn->query($sql);
        $result->setFetchMode($mode);
        $re=$result->fetchAll();
        $result=null;
        return$re;
    }

    /*查询指定表中有多少条记录*/publicfunctiongetTabRows($key,$tableName,$where)
    {$sql="select count(".$key.") as 'c' from ".$tableName." where ".$where."";
        $result=$this->conn->query($sql);
        $result->setFetchMode(PDO::FETCH_ASSOC);
        $re=$result->fetch();
        $result=null;
        return intval($re['c']);
    }

    /*关闭数据库*/publicfunctioncloseConn()
    {$this->conn=null;
    }
}

?>

下面是我调用这个类的代码


header("content-type:text/html;charset=utf-8");
include"sqlcontrol.class.php";
$config["host"]="localhost";
$config["dbname"]="biaodan";
$config["user"]="root";
$config["pwd"]="";
$config["charset"]="utf-8";

$db=new dbPdoManger($config);

$sql="INSERT INTO `test` (`name`, `nicheng`, `password`, `sex`, `icon`, `cardid`, `city`, `phone`, `qq`, `mail`, `liuyan`) 
     VALUES ('t', 't', 't', 't, 't', '1315', 'tttt', '598562', '79874564', 'tret', 'werterter')";

echo$db->execSql($sql);

求大神解救。。。

以上就介绍了小白求助:php数据库出错,Fatal error: Call to a member function exec() on a non-object in,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

人气教程排行