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

时间:2017-08-15 11:22:35

标签: php mysql

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

的index.php:

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

错误是:

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

config.php的一部分:

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

最后是mysql.php:

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

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

1 个答案:

答案 0 :(得分:0)

调用over_lay中的函数

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

<强>更新 我只是改变了

$mysqli->connect();

$mysqli = connect();