简体   繁体   中英

Php MySql connection issue

I have db.php with the following code.

<?php
$dbHost = "localhost";
$dbUser = "root";
$dbPass = "password";
$dbName = "test";
$databaseLink = new mysqli ($dbHost, $dbUser, $dbPass);
if($databaseLink)
{
    mysqli_select_db($databaseLink,$dbName);
}
?>

which I usually import on to other php page like this

<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/core/include/db.php";
?>

and works fine. I can start querying using $databaseLink . But there is one page where its not working. But if I explicitly define the connection like this $databaseLink= mysqli_connect("localhost", "root", "password", "test"); it works. There are other php files in the same directory which has no issues.

I have tried

<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/core/include/db.php";
global($databaseLink);
?>

But that does not seem to work too. I have looked it up online for examples but can find any help.

您忘记要求您的db.php文件

require_once($path);

您可以简单地使用php代码顶部的include链接到另一个php页面。

include '/core/include/db.php';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM