This error came out when I tried to connect to a mysql database:

A PHP Error was encountered Severity: Warning

Message: mysqli::real_connect(): (HY000/1044): Access denied for user ''@'localhost' to database 'calendar_events'

Filename: mysqli/mysqli_driver.php

Line Number: 203

Backtrace:

File: C:\xampp2\htdocs\fullcalendar\application\controllers\Calendar.php Line: 7 Function: __construct

File: C:\xampp2\htdocs\fullcalendar\index.php Line: 315 Function: require_once

1.index.php (view)

<html lang="en">
    <head>
        <title>Calendar Display</title>
        <script src="<?php echo base_url(); ?>assets/fullcalendar/lib/moment.min.js"></script>
        <script src="<?php echo base_url(); ?>assets/fullcalendar/lib/jquery-ui.min.js"></script>
        <script src="<?php echo base_url(); ?>assets/fullcalendar/lib/jquery.min.js"></script>
        <link rel="stylesheet" href="<?php echo base_url(); ?>assets/fullcalendar/fullcalendar.min.css" />
        <script src="<?php echo base_url(); ?>assets/fullcalendar/fullcalendar.min.js"></script>
        <script src="<?php echo base_url(); ?>assets/fullcalendar-3.9.0/locale/es.js"></script>
    </head>
    <body>
    <div class="container">
    <div class="row">
    <div class="col-md-12">
    <h1>Calendar</h1>
    <div id="calendar">

    </div>
    </div>
    </div>
    </div>
    <!--<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>-->
    <script type="text/javascript">
        $(document).ready(function() {
        $('#calendar').fullCalendar({
           eventSources: [
             {
                 events: function(start, end, timezone, callback) {
                     $.ajax({
                     url: '<?php echo base_url() ?>calendar/get_events',
                     dataType: 'json',
                     data: {
                     // our hypothetical feed requires UNIX timestamps
                     start: start.unix(),
                     end: end.unix()
                     },
                     success: function(msg) {
                         var events = msg.events;
                         callback(events);
                     }
                     });
                 }
             },
         ]            

        });
        });
    </script>
    <style>
            #calendar{
                width: 800px;
                margin: 0px auto;
            }
    </style>

    </body>
</html>
  1. Calendar_model.php

class Calendar_model extends CI_Model {

    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    public function get_events($start, $end){

        return $this->db->where("start >=", $start)->where("end <=", $end)->get("calendar_events");
    }

    public function add_event($data)
    {
        $this->db->insert("calendar_events", $data);
    }

    public function get_event($id)
    {
        return $this->db->where("ID", $id)->get("calendar_events");
    }

    public function update_event($id, $data)
    {
        $this->db->where("ID", $id)->update("calendar_events", $data);
    }

    public function delete_event($id)
    {
        $this->db->where("ID", $id)->delete("calendar_events");
    }

}
  1. Calendar.php (controller)

    class Calendar extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model("calendar_model");
    }
    
    public function index()
    {
        $this->load->helper('url');
        $this->load->view('index', array());
    }
    
     public function get_events()
     {
         // Our Start and End Dates
         $start = $this->input->get('start');
         $end = $this->input->get('end');
    
         $startdt = new DateTime('now'); // setup a local datetime
         $startdt->setTimestamp($start); // Set the date based on timestamp
         $start_format = $startdt->format('Y-m-d H:i:s');
    
         $enddt = new DateTime('now'); // setup a local datetime
         $enddt->setTimestamp($end); // Set the date based on timestamp
         $end_format = $enddt->format('Y-m-d H:i:s');
    
         $events = $this->calendar_model->get_events($start_format, $end_format);
    
         $data_events = array();
    
         foreach($events->result() as $r) {
    
             $data_events[] = array(
                 "id" => $r->ID,
                 "title" => $r->title,
                 "description" => $r->description,
                 "end" => $r->end,
                 "start" => $r->start
             );
         }
    
         echo json_encode(array("events" => $data_events));
         exit();
     }
    

    }

2 Answers

0
DFriend On

The problem is probably not in any code you show. The error message is saying that the user you specified in the database configuration (localhost) does not have permission to use the calendar_events database.

This is a database setup issue and not a CodeIgniter issue.

How you adjust user permission for your database will depend on the tools you have available to administer your database.

-1
F. Vandroy On

I think you should check your database configuration in "application/config/database.php".

The problem is probably in this file.

Tell me if it's work