首頁  >  文章  >  後端開發  >  小白求救:php資料庫出錯,Fatal error: Call to a member function exec() on a non-object in

小白求救:php資料庫出錯,Fatal error: Call to a member function exec() on a non-object in

WBOY
WBOY原創
2016-08-08 09:28:553031瀏覽

今天整理了下以前的pdo資料庫封裝類,但是在使用的時候報錯,Fatal error: Call to a member function exec() on a non-object in sqlcontrol.class.php on line 45 這個是完整的錯誤提示,在以前沒有修改的時候沒有這個錯誤,這是怎麼回事兒。
這個是我用的類別

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

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

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

    <span>/*
     * 增删改
     */</span><span>public</span><span><span>function</span><span>execSql</span><span>(<span>$sql</span>)</span>
    {</span><span>$bool</span>=<span>$this</span>->conn->exec(<span>$sql</span>);
        <span>if</span>(<span>$bool</span>><span>0</span>)
        {
            <span>return</span><span>true</span>;
        }<span>else</span>
        {
            <span>return</span><span>false</span>;
        }
    }

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

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

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

    <span>/*关闭数据库*/</span><span>public</span><span><span>function</span><span>closeConn</span><span>()</span>
    {</span><span>$this</span>->conn=<span>null</span>;
    }
}

<span>?></span></span></code>

下面是我呼叫這個類別的程式碼

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

<span>$db</span>=<span>new</span> dbPdoManger(<span>$config</span>);

<span>$sql</span>=<span>"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')"</span>;

<span>echo</span><span>$db</span>->execSql(<span>$sql</span>);</span></span></code>

求大神解救。 。 。

以上就介紹了小白求助:php資料庫出錯,Fatal error: Call to a member function exec() on a non-object in,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn