Welcome Guest, Not a member yet? Register   Sign In
SQL query executed 3 times (not wanted)
#1

[eluser]Jelenik[/eluser]
Hi guys,

i am experiencing a strange behaviour. I want to insert one row to database everytime is shown detail of video. But sql query seems to be executed 3 times - in profiler there is only 1 sql. I tried everything - i tried to remove all code which i dont use - only problematic part remains. I simplified functions - for example that function $this->Hlavny->zvys_pocet_zobrazeni do more (it actually updates detail of video too, but thats not important - it creates duplicity sql queries with simplified version too). It seems that there are some redirects or something, because i am experiencing this only when my url ends with slash. Lets get to the point.

My routes.php
Code:
$route['default_controller'] = "something";
$route['(:any)'] = "something";
- as you can see - everything is redirected to one controller and actions are based on segments - i had to use this because client want to have everything in first segment - categories, detail and so on - so i have to check if first segment isnt category and if isnt it will be propably video...
- i tried to use new feature and force to use only that one controller through settings in main controller index.php - but it didnt work either - and i found that there is a error with example -> There is: The controller class file name. Example: Mycontroller.php -> but it didnt work with .php at the end for me - i dont know if it depends on server on which codeigniter runs or its only bug.

I have this controller:

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Something extends CI_Controller {

    function __construct()
    {
        parent::__construct();
    }

    function index()
    {
            $data = $this->project->inicializacia();
            $data['kategorie'] = $this->Hlavny->kategorie();

            if ($this->uri->segment(1) != '') {
                    $seo = $this->uri->segment(1);
                    $data['polozka'] = $this->Hlavny->video(array('seo'=>$seo));
                    $this->Hlavny->zvys_pocet_zobrazeni($data['polozka']->id);
                    $this->load->view('header', $data);
                    $this->load->view('detail_videa', $data);
                    $this->load->view('footer', $data);

            }

    }

}

/* End of file */

$this->project->inicializacia() - this is simple inicialization function, which is used only for setting up meta tags + profiler as you can see

Code:
function inicializacia()
    {
            $default_title = 'Example.com';
            $data['meta_tagy']['title'] = 'The best - '.$default_title;
            $data['meta_tagy']['description'] = 'You you :)';
            $data['meta_tagy']['kw'] = 'kw1, kw2';

            if (($_SERVER['HTTP_HOST'] == 'localhost' OR $_SERVER['HTTP_HOST'] == '127.0.0.1') AND ($this->_CI->uri->segment(1) != 'ajaxik')) $this->_CI->output->enable_profiler(TRUE);
            if ($_SERVER['HTTP_HOST'] == 'localhost' OR $_SERVER['HTTP_HOST'] == '127.0.0.1') $data['localhost'] = TRUE;
            else $data['localhost'] = FALSE;

            return $data;
    }

I am using model Hlavny.php:
Code:
<?php

class Hlavny extends CI_Model {

    function __construct()
    {
        parent::__construct();
        $this->load->database();
    }
    
    function kategorie()
    {
        $polozky = $this->db->get_where('kategorie');
        if ($polozky->num_rows() > 0) return $vysledok;
        else return FALSE;
    }

    function video($where = array())
    {
        $polozka = $this->db->get_where('videa',$where);
        if ($polozka->num_rows() == 1) {
            $polozka = $polozka->row();
            return $polozka;
        }
        else return FALSE;
    }

    function zvys_pocet_zobrazeni($video_id)
    {
         $this->db->insert('videa_zobrazenia', array('video_id'=>$video_id, 'ip'=>$this->input->ip_address(),'cas'=>date("Y-m-d H:i:s", time())));
    }

}

/* End of file hlavny.php */

I found that there are 2 similiar thread on this forum with this kind of issue (both arent resolved as you can see):
http://ellislab.com/forums/viewthread/68585/
http://ellislab.com/forums/viewthread/64160/

Knows somebody where can be that problem?

I have lost 2 hours trying everything in code and on the end i noticed that without slash on the end it works - but this is not solution.
#2

[eluser]oldmatt[/eluser]
I don't know if this is your problem, but I have had problems in the past with routes because I was not careful about how I defined the routes.

URI Routing

That page in the documentation shows in the pink note box, that the order in which you define routes affects the results.

Again, I didn't really extensively look into your code, but I thought it might be a good place to start.
#3

[eluser]Jelenik[/eluser]
Oldmatt - thanks for reply - i think that it must be something with routing too, but if you look to my routes.php, there are only 2 rules:

Code:
$route['default_controller'] = "something";
$route['(:any)'] = "something";

- i dont see any problem there, so i dont know why the hell is this happening Smile
#4

[eluser]Jelenik[/eluser]
After many of hours trying everything, i think that i find where is problem.
I had in view image with relative path(i just received design from designer and i didnt noticed that there is image with relative path - i am using absolute paths for images), so adding slash on the end will break that path. And when there is a problem with path for image - it creates duplicates sql queries - i think that it is very strange - or it is a feature? Smile I dont know, but i believe that this is a very big bug.

I have created bugtest application with which you can see yourself.

You can download it here:
http://bugtest.gigavideo.sk/script.zip
Database structure + simple data here:
http://bugtest.gigavideo.sk/bug_test.sql

In application/database.php is set username as root and no password - assume that you want to test in on localhost - or you can just edit it for your needs as you wish.

Maybe you will need to edit .htaccess - for example in XAMPP i have this .htaccess:
Code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

And on live server i have:
Code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

Or you can just test it live here:
http://bugtest.gigavideo.sk/

Just try both variants which are offered - with slash on the end and without and then click on third link which will show you inserted rows for your IP...
#5

[eluser]WanWizard[/eluser]
It's the downside of using 'lazy' rewrite rules.

Your rules basically say "rewrite everything that isn't a file or a directory to index.php".
Which means that every typo or missing asset will generate a second request of index.php.

Store your assets in a separate /assets folder, and exclude that from rewriting to avoid issues like this.
#6

[eluser]Jelenik[/eluser]
Thanks a lot WanWizard for clarification.
Its shame that i forget to think about that redirects.

But again thanks!




Theme © iAndrew 2016 - Forum software by © MyBB