Welcome Guest, Not a member yet? Register   Sign In
Query execution behaves differently when I use Input core library
#1

Hello guys! I am new to CodeIgniter and I have started implemention with it! I like it.

 I am facing a strange problem here and I will need you help. I will attach my code and logs:

Example correct:
PHP Code:
    public function searchTest() {
        
$myparams = array();
        
$myparams[] = 200;

        
$sql "
            SELECT
                    id,
                    title
            FROM
                    movies
            WHERE length > ?
        "
;

        
$q $this->db->query($sql$myparams);
        
log_message('debug''myparams: '.print_r($myparamstrue));
        
log_message('debug''Rows: '.count($q->result_array()));
    } 

Logs of correct example:
Quote:05/10 02:40:03.229 - INFO  --> Config Class Initialized
05/10 02:40:03.229 - INFO  --> Hooks Class Initialized
05/10 02:40:03.229 - DEBUG --> UTF-8 Support Enabled
05/10 02:40:03.229 - INFO  --> Utf8 Class Initialized
05/10 02:40:03.245 - INFO  --> URI Class Initialized
05/10 02:40:03.245 - INFO  --> Router Class Initialized
05/10 02:40:03.245 - INFO  --> Output Class Initialized
05/10 02:40:03.245 - INFO  --> Security Class Initialized
05/10 02:40:03.245 - DEBUG --> Global POST, GET and COOKIE data sanitized
05/10 02:40:03.245 - INFO  --> Input Class Initialized
05/10 02:40:03.245 - INFO  --> Language Class Initialized
05/10 02:40:03.245 - INFO  --> Loader Class Initialized
05/10 02:40:03.245 - DEBUG --> Config file loaded: C:\wamp\www\ignatios\application\config/myconfig.php
05/10 02:40:03.260 - INFO  --> Helper loaded: url_helper
05/10 02:40:03.260 - INFO  --> Helper loaded: tools_helper
05/10 02:40:03.260 - INFO  --> Helper loaded: cookie_helper
05/10 02:40:03.260 - INFO  --> Database Driver Class Initialized
05/10 02:40:03.276 - DEBUG --> Session: "sess_save_path" is empty; using "session.save_path" value from php.ini.
05/10 02:40:03.276 - INFO  --> Session: Class initialized using 'files' driver.
05/10 02:40:03.276 - INFO  --> Controller Class Initialized
05/10 02:40:03.276 - DEBUG --> Class is = Movies
05/10 02:40:03.276 - INFO  --> Model Class Initialized
05/10 02:40:03.276 - DEBUG --> myparams: Array
(
    [0] => 200
)

05/10 02:40:03.276 - DEBUG --> Rows: 1                       <--- Correct
05/10 02:40:03.276 - INFO  --> Final output sent to browser
05/10 02:40:03.276 - DEBUG --> Total execution time: 0.0469


Now here is the Strange/Incorrect example:
PHP Code:
    public function searchTest() {
        
$myparams = array();
        
$myparams[] = $this->input->get('length');

        
$sql "
            SELECT
                    id,
                    title
            FROM
                    movies
            WHERE length > ?
        "
;

        
$q $this->db->query($sql$myparams);
        
log_message('debug''myparams: '.print_r($myparamstrue));
        
log_message('debug''Rows: '.count($q->result_array()));
    } 

Here are the logs of the incorrect example:
Quote:05/10 02:39:18.588 - INFO  --> Config Class Initialized
05/10 02:39:18.588 - INFO  --> Hooks Class Initialized
05/10 02:39:18.588 - DEBUG --> UTF-8 Support Enabled
05/10 02:39:18.588 - INFO  --> Utf8 Class Initialized
05/10 02:39:18.588 - INFO  --> URI Class Initialized
05/10 02:39:18.604 - INFO  --> Router Class Initialized
05/10 02:39:18.604 - INFO  --> Output Class Initialized
05/10 02:39:18.604 - INFO  --> Security Class Initialized
05/10 02:39:18.604 - DEBUG --> Global POST, GET and COOKIE data sanitized
05/10 02:39:18.604 - INFO  --> Input Class Initialized
05/10 02:39:18.604 - INFO  --> Language Class Initialized
05/10 02:39:18.604 - INFO  --> Loader Class Initialized
05/10 02:39:18.604 - DEBUG --> Config file loaded: C:\wamp\www\ignatios\application\config/myconfig.php
05/10 02:39:18.604 - INFO  --> Helper loaded: url_helper
05/10 02:39:18.620 - INFO  --> Helper loaded: tools_helper
05/10 02:39:18.620 - INFO  --> Helper loaded: cookie_helper
05/10 02:39:18.620 - INFO  --> Database Driver Class Initialized
05/10 02:39:18.635 - DEBUG --> Session: "sess_save_path" is empty; using "session.save_path" value from php.ini.
05/10 02:39:18.635 - INFO  --> Session: Class initialized using 'files' driver.
05/10 02:39:18.635 - INFO  --> Controller Class Initialized
05/10 02:39:18.635 - DEBUG --> Class is = Movies
05/10 02:39:18.635 - INFO  --> Model Class Initialized
05/10 02:39:18.651 - DEBUG --> myparams: Array
(
    [0] => 200
)

05/10 02:39:18.651 - DEBUG --> Rows: 912                     <--- Wrong
05/10 02:39:18.651 - INFO  --> Final output sent to browser
05/10 02:39:18.651 - DEBUG --> Total execution time: 0.0625



The only difference in these two examples is this:
Quote:$myparams[] = 200;

$myparams[] = $this->input->get('length');

The first example returns only 1 row in the query result, while the second example returns an incorrect number of rows.

What is happening here ? Huh

(If you have time, please have a local test and provide feedback)
Reply
#2

(This post was last modified: 10-05-2017, 02:58 AM by g4nar.)

OK guys, the solution is this:


Quote:$myparams[] = (int) $this->input->get('length');



Previously the CI treated $this->input->get('length') as a string.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB