0

I'm having a problem regarding my database connection not recognized on my functions.php even though i have included it already. here's the code.

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'];  
}?>

im still getting the "Notice: Undefined variable: conn" error when i use the function on my page.

  • 1
    This is probably a variable scoping issue. The `random_banner()` function is looking for a variable called `$conn` defined inside that function, but you defined it in the global scope in the other file. Try adding a new line `global $conn;` as the first line inside the `random_banner()` function. – Ian Drake Jan 03 '17 at 18:41
  • thanks for the explanation. i did figure it out. – Deviruchi D Devourer Jan 03 '17 at 21:24

0 Answers0