• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

php 搞代码 3年前 (2022-01-23) 13次浏览 已收录 0个评论

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

<!---ecms 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

<!---ecms 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.ph<i>本文@来#源gaodai$ma#com搞$$代**码网</i><strong>搞代gaodaima码</strong>p");                }                else                     header("Location:../view/errors.php?value=tianjia");            }            break;       }

——解决方案——————–
public function __construct($configFile=”config.inc.php”) //两个下划线


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php用mysqli模块连数据库Fatal error: Call to a member function query() 。解决思路
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址