CodeIgniter Forums
sql problem - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: sql problem (/showthread.php?tid=30381)



sql problem - El Forum - 05-12-2010

[eluser]elmne[/eluser]
I have this problem while trying to run a query in a class/model

Code:
$this->customer_type_id = $this->input->post('customer_type_id');
        $this->customer_type = $this->input->post('customer_type');
        
        
        if ( !( $this->input->post('customer_type_id')))
        {
           $query = $this->db->query('SELECT customer_type_id, customer_type FROM customer_type WHERE customer_type = "$this->customer_type" AND record_status IS NULL');
        }
        else
        {
         $query = $this->db->query('SELECT customer_type_id, customer_type FROM customer_type WHERE customer_type_id = "$this->customer_type_id" AND record_status IS NULL');
        }


The query fails to produce a result,

But when i run the sql directly against the database, taking out the variable, like this

Code:
SELECT customer_type_id, customer_type FROM customer_type WHERE customer_type_id = "$this->customer_type_id" AND record_status IS NULL

I get a result.

What's the cause of the failure to return a result?


sql problem - El Forum - 05-12-2010

[eluser]Bart v B[/eluser]
why not direct youre inputs in youre query?
Mabe i am wrong but why are you naming youre table and column the same?

Code:
$this->customer_type_id = $this->input->post('customer_type_id');
      
        
        if ( !( $this->input->post('customer_type_id')))
        {
           $query = $this->db->query("SELECT customer_type_id,
                                                      customer_type
                                                      FROM
                                                      customer_type
                                                      WHERE
                                                      customer_type = '".$this->input->post('customer_type')."'
                                                      AND
                                                      record_status IS NULL');
        }



sql problem - El Forum - 05-13-2010

[eluser]CleverMind[/eluser]
hello,

First you have to write query in sql of phpmyadmin and if query runs then that query write in your model. your problem will be solved.


sql problem - El Forum - 05-13-2010

[eluser]danmontgomery[/eluser]
strings with single quotes are interpreted literally. You need to either concat the variable or use double quotes.

Code:
$var = 'some value';
echo 'var is $var'; // output: var is $var
echo "var is $var"; // output: var is some value
echo 'var is '.$var; // output: var is some value



sql problem - El Forum - 05-17-2010

[eluser]elmne[/eluser]
Thanks for the tips. I realised single quotes were the problem