duan19850312
2017-08-15 11:22
浏览 189
已采纳

致命错误:在null上调用成员函数connect()

Trying to include function "overlay" from config.php into other php pages.

Index.php:

<?php include("./config/config.php");
over_lay(); ?>
//// html code ////

The error is:

Fatal error: Call to a member function connect() on null in ...\config\config.php on line 182

Part of config.php:

include("./config/mysql.php");
function over_lay()
{
$mysqli->connect();
?>
//// html code with partly mysqli requests ////
<?php
  $mysqli->close();
}
?>

And finally mysql.php:

<?php
function connect() 
{
$mysqli = new mysqli("localhost","root","","database_name");
$mysqli->query("SET NAMES utf8");
return $mysqli;
}
?>

I'm testing this on OpenServer and of course I do have the database.

图片转代码服务由CSDN问答提供 功能建议

尝试将config.php中的函数“overlay”包含到其他php页面中。

Index.php:

 &lt;?php include(“./ config / config.php”); 
over_lay()  ;  ?&gt; 
 //// html代码//// 
   
 
 

错误是:

  致命错误:在第182行的\ _config \ config.php中调用null上的成员函数connect()  
 
 

config.php的一部分:< /p>

include("./config/mysql.php");
function over_lay()
 {
 $ mysqli-&gt; connect(); 
?&gt;  
 ////包含部分mysqli请求的HTML代码//// 
&lt;?php 
 $ mysqli-&gt; close(); 
} 
?&gt; 
    
 
 

最后是mysql.php:

 &lt;?php 
function connect()
 {
 $ mysqli = new mysqli(“localhost”  ,“root”,“”,“database_name”); 
 $ mysqli-&gt; query(“SET NAMES utf8”); 
return $ mysqli; 
} 
?&gt; 
   
 
 

我在OpenServer上测试它,当然我有数据库。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • duanlai1855 2017-08-15 11:24
    最佳回答

    Call the function inside your over_lay

    include("./config/mysql.php");
    function over_lay()
    {
        $mysqli = connect();
         ?>
         //// html code with partly mysqli requests ////
         <?php
         $mysqli->close();
     }
    

    Update I simply changed

    $mysqli->connect();

    to

    $mysqli = connect();

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题