Home > Article > Backend Development > Newbie asking for help: php database error, Fatal error: Call to a member function exec() on a non-object in
Today I sorted out the previous pdo database encapsulation class, but when using it, an error was reported. Fatal error: Call to a member function exec() on a non-object in sqlcontrol.class.php on line 45. This is a complete error. Tip, this error did not occur when there was no modification before. What is going on?
This is the class I use
<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>
The following is the code I use to call this class
<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>
Please help me. . .
The above introduces the beginner's help: PHP database error, Fatal error: Call to a member function exec() on a non-object in, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.