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

致命错误:在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.

  • 写回答

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();

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?