0

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();
     }
    

    }

ADyson
  • 57,178
  • 14
  • 51
  • 63
berlin
  • 9
  • 4
  • almost none of this code is relevant to your error. The failure is in "mysqli/mysqli_driver.php", as the error message says. It seems you have used the wrong username and password, or that the username you've used does not have permission to use the calendar_events database. You'll have to check your setup. Since we can't see the relevant code, or your database setup, we can't really help you with that. – ADyson May 12 '19 at 20:32
  • check your database config file :- /application/config/database.php – madara uchiha May 13 '19 at 04:15

2 Answers2

0

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.

DFriend
  • 8,869
  • 1
  • 13
  • 26
-1

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

F. Vandroy
  • 122
  • 2
  • 15