오늘은 이전 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 데이터베이스 오류, 치명적인 오류: 객체가 아닌 멤버 함수 exec() 호출 내용을 포함하여 PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.