Welcome Guest, Not a member yet? Register   Sign In
sql problem
#1

[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?
#2

[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');
        }
#3

[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.
#4

[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
#5

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




Theme © iAndrew 2016 - Forum software by © MyBB