我的函数无法识别数据库连接

时间:2017-01-03 18:37:21

标签: php mysqli database-connection

即使我已将其包含在我的functions.php中,但我的数据库连接无法识别。这是代码。

dbconnect.php

<?php
define ("DB_SERVER","localhost");
define ("DB_USER","root");
define ("DB_PASS","");
define ("DB_NAME","hookdb");

$conn  = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
//test connection
if(mysqli_connect_errno()){
    die("Database connection failed: " .mysqli_connect_error() . "( " . mysqli_connect_errno(). ")");
}?>

的functions.php

<?php
require_once("dbconnect.php");
function check_query($result_set){
        if(!$result_set){
            die("Database query failed. ");         
        }   
}

function random_banner(){
    $query = "SELECT COUNT(*) as total FROM manga_name";
    $result = mysqli_query($conn, $query);      
    check_query($result);
    $data = mysqli_fetch_assoc($result);
    return($data);
    //echo $data['total'];  
}?>

我仍然得到&#34;注意:未定义的变量:conn&#34;我在页面上使用该功能时出错。

0 个答案:

没有答案