首页 >后端开发 >php教程 >php用mysqli模块连数据库Fatal error: Call to a member function query() 。解决思路

php用mysqli模块连数据库Fatal error: Call to a member function query() 。解决思路

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB原创
2016-06-13 10:50:501520浏览

php用mysqli模块连数据库Fatal error: Call to a member function query() 。。。。
出现了这个错误:Fatal error: Call to a member function query() on a non-object in D:\wamp\www\bills\model\UserDB.class.php on line 24
代码如下:

PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><?php class Database    {        var $mysqli;        var $showError;                public function _construct($configFile="config.inc.php")        {            require_once($configFile);            $this->mysqli=new mysqli($dbhost,$dbuser,$dbpwd,$dbname);            if(mysqli_connect_errno())            {                echo "物品数据库连接失败!";            }        }        public function close()        {            if($this->mysqli)                $this->mysqli->close();            $this->mysqli=FALSE;        }        public function _destruct()        {            $this->close();        }    }    class UserDB extends Database    {        public function queryUserByName($name)        {            $query="SELECT * FROM Users WHERE name='".$name."'";            if ($result=$this->mysqli->query($query))  //此处提示出错!!                return true;            else                 return false;        }


然后在另一个页面里调用上面的类对象:
PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><?php function __autoload($className)    {        include_once "../model/".$className.".class.php";    }    $udb=new UserDB();   //明明初始化了,可是到类里面就出错?    $idb=new ItemsDB();       echo "jinlaile";//    if(isset($_COOKIE("userInfo")))//    {//        $userInfo=unserialize($_COOKIE("userInfo"));//    }    switch($_GET["action"])    {        case "register":                        $lvalue=$_POST;            $rtmp=$udb->queryUserByName($lvalue["name"]);            echo "alert";            if($rtmp)  //这个名字没有注册            {                $rusers=new User($lvalue);                if($udb->addUser($rusers))                {                    setcookie("start",0);                    setcookie("userInfo",serialize($rusers));                    setcookie("username",$rusers->name);                    header("Location:../view/index.php");                }                else                     header("Location:../view/errors.php?value=tianjia");            }            break;       }


------解决方案--------------------
public function __construct($configFile="config.inc.php") //两个下划线
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn