首頁 >後端開發 >php教程 >PDO——Call to a member function query() on a non-object

PDO——Call to a member function query() on a non-object

WBOY
WBOY原創
2016-06-06 20:16:431479瀏覽

<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

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