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

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

报告相同问题?

悬赏问题

  • ¥200 如何使用postGis实现最短领规划?
  • ¥15 pyinstaller打包错误
  • ¥20 cesm的气溶胶排放文件
  • ¥15 逐月累计,月份不连续,补齐月份
  • ¥15 应用简单的Python代码完成一个学生成绩管理系统
  • ¥15 用matlab求微分方程初值问题
  • ¥15 vscode下编写第三方库opencv与pcl代码时没有代码提示
  • ¥15 能够跑通不报错,如何解决?(标签-matlab)
  • ¥15 MOS在RDS较大,频率高时开关波形异常
  • ¥15 SCENIC分析报错求解答