0

Notice: Undefined variable: con in D:\xampp\htdocs\tanmay.com\p\andooze\include\dbconfig.php on line 15

Fatal error: Uncaught Error: Call to a member function query() on null in D:\xampp\htdocs\tanmay.com\p\andooze\include\dbconfig.php:15 Stack trace: #0 D:\xampp\htdocs\tanmay.com\p\andooze\include\front_header.php(3): require() #1 D:\xampp\htdocs\tanmay.com\p\andooze\index.php(2): require('D:\xampp\htdocs...') #2 {main} thrown in D:\xampp\htdocs\tanmay.com\p\andooze\include\dbconfig.php on line 15

my dbconfig.php file is

<?php
if (session_status() == PHP_SESSION_NONE) {
      session_start();
   }

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
try {
  //Username, Password and Database
  $con = new mysqli("localhost", "tanmayna_andooze", "andooze123@", "tanmayna_andooze");
  $con->set_charset("utf8mb4");
} catch(Exception $e) {
  error_log($e->getMessage());
  //Should be a message a typical user could understand
}
$fset = $con->query("select * from setting")->fetch_assoc();
$fetch_main = $con->query("select * from main_setting")->fetch_assoc();

date_default_timezone_set($fset['timezone']);
$dirname = dirname( dirname(__FILE__) ).'/api';
$dirname = dirname( dirname(__FILE__) ).'/rapi';

?>

my front_header.php is:

<?php 
session_start();
require 'dbconfig.php';
?>
<?php
//if($_SERVER['REQUEST_URI'] !='/activate.php')
{
if(empty($_SESSION['username']))
{

}
else 
{
?>
<script>
    window.location.href="dashboard.php";
</script>
<?php 
}
}
?>
<!DOCTYPE html>
<html lang="en" class="loading">
  
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
     <meta name="viewport" content="width=device-width, initial-scale=1" />
     <?php
if($_SERVER['REQUEST_URI'] !='/activate.php')   
{   
     ?>
    <title>Login Page - <?php echo $fset['title'];?></title>
<?php } else {?>
 <title>Verify Page - <?php echo $fset['title'];?></title>
<?php } ?>
    <link rel="shortcut icon"  href="<?php echo $fset['favicon'];?>">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-touch-fullscreen" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="default">
    <link href="https://fonts.googleapis.com/css?family=Rubik:300,400,500,700,900|Montserrat:300,400,500,600,700,800,900" rel="stylesheet">
    
    <link rel="stylesheet" type="text/css" href="app-assets/fonts/feather/style.min.css">
    <link rel="stylesheet" type="text/css" href="app-assets/fonts/simple-line-icons/style.css">
    <link rel="stylesheet" type="text/css" href="app-assets/fonts/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="app-assets/vendors/css/perfect-scrollbar.min.css">
    <link rel="stylesheet" type="text/css" href="app-assets/vendors/css/prism.min.css">
    
    <link rel="stylesheet" type="text/css" href="app-assets/css/app.css">
   
  </head>
Dharman
  • 30,962
  • 25
  • 85
  • 135

1 Answers1

0

If the assignment to $con fails, $con won't be set. So you need to exit the script in the catch block to prevent trying to continue without $con.

<?php
if (session_status() == PHP_SESSION_NONE) {
      session_start();
   }

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
try {
  //Username, Password and Database
  $con = new mysqli("localhost", "tanmayna_andooze", "andooze123@", "tanmayna_andooze");
  $con->set_charset("utf8mb4");
} catch(Exception $e) {
  error_log($e->getMessage());
  //Should be a message a typical user could understand
  die("Unable to connect to database");
}

$fset = $con->query("select * from setting")->fetch_assoc();
$fetch_main = $con->query("select * from main_setting")->fetch_assoc();

date_default_timezone_set($fset['timezone']);
$dirname = dirname( dirname(__FILE__) ).'/api';
$dirname = dirname( dirname(__FILE__) ).'/rapi';
?>

Of course, you also need to figure out why you can't connect to the database and fix that.

Barmar
  • 741,623
  • 53
  • 500
  • 612