Heim >Backend-Entwicklung >PHP-Tutorial >PDO——Call to a member function query() on a non-object
<code>Think PHP在3.2之后好像就不支持PDO扩展了,然后我为了用PDO在控制层中写了一个php文件,里面只是写了实例化PDO的一段代码,我在控制请中引入这个文件,刚开始连接查询数据的时候没有错误,但是好像是有返回值的时候就报了一个——“**Call to a member function query() on a non-object**”的错误,有哪位了解的,请教下</code>
<code>pdo.class.php <?php $PDO = new PDO('mysql:host=localhost;dbname=cxshop', 'root', '',array( PDO::ATTR_PERSISTENT => true)); ?></code>
<code>CommonController.class.php public function checkPDO($rolename,$mid=1){ if (intval($mid) > 0) { require_once('Pdo.class.php'); $res = $PDO->query("SELECT r_id as rid FROM `shop_role` where r_method='$rolename' limit 1"); // $roleres = $res->fetch(2); $checkmethID = $roleres['rid']; // dump($checkmethID);exit; $res = $PDO->query("SELECT gid FROM `shop_ag_access` where mid='$mid'"); $agres = $res->fetchAll(2); if($checkmethID){ foreach ($agres as $key => $value) { $rges = $PDO->query("SELECT rid FROM `shop_rg_access` where gid='".$value['gid']."'"); $rges = $rges->fetchAll(2); if (!$rges) { return fasle; } $rgarr = array(); foreach ($rges as $kk => $val) { $rgarr[] = $val['rid']; } $agres[$key]['gid'] = $rgarr; } $PDO = null; $arr = array(); foreach ($agres as $key => $value) { $arr[] = $value['gid']; } foreach ($arr as $key => $value) { if (in_array($checkmethID, $value) == true) { return true; } } return false; }else{ return false; } }else{ return false; } } </code>
上面的代码可以忽略不看,就是Think PHP链接PDO的问题
<code>Think PHP在3.2之后好像就不支持PDO扩展了,然后我为了用PDO在控制层中写了一个php文件,里面只是写了实例化PDO的一段代码,我在控制请中引入这个文件,刚开始连接查询数据的时候没有错误,但是好像是有返回值的时候就报了一个——“**Call to a member function query() on a non-object**”的错误,有哪位了解的,请教下</code>
<code>pdo.class.php <?php $PDO = new PDO('mysql:host=localhost;dbname=cxshop', 'root', '',array( PDO::ATTR_PERSISTENT => true)); ?></code>
<code>CommonController.class.php public function checkPDO($rolename,$mid=1){ if (intval($mid) > 0) { require_once('Pdo.class.php'); $res = $PDO->query("SELECT r_id as rid FROM `shop_role` where r_method='$rolename' limit 1"); // $roleres = $res->fetch(2); $checkmethID = $roleres['rid']; // dump($checkmethID);exit; $res = $PDO->query("SELECT gid FROM `shop_ag_access` where mid='$mid'"); $agres = $res->fetchAll(2); if($checkmethID){ foreach ($agres as $key => $value) { $rges = $PDO->query("SELECT rid FROM `shop_rg_access` where gid='".$value['gid']."'"); $rges = $rges->fetchAll(2); if (!$rges) { return fasle; } $rgarr = array(); foreach ($rges as $kk => $val) { $rgarr[] = $val['rid']; } $agres[$key]['gid'] = $rgarr; } $PDO = null; $arr = array(); foreach ($agres as $key => $value) { $arr[] = $value['gid']; } foreach ($arr as $key => $value) { if (in_array($checkmethID, $value) == true) { return true; } } return false; }else{ return false; } }else{ return false; } } </code>
上面的代码可以忽略不看,就是Think PHP链接PDO的问题
“on a non-object”说明你调用了对象不存在的方法,或这个对象没有实例化。检查下代码,留意在调用对象的方法的位置。
试试把query换做exec