-1

I am tring to validate that there is a record for a name on a csv file. I am using an sql statement to look to see if that name exist, but i get this error

Error Number: 1054

Unknown column 'aa' in 'where clause'

SELECT name FROM company WHERE name =aa


 public function findCompany($company){
    $query = $this->db->query("SELECT name FROM companyWHERE name =".$company);
    if($query->num_rows() > 0){
        return true;
    }else{
        return false;
    }
}

Could it be an isue with attaching a variable to the end of the string?

John Doe
  • 3
  • 4

3 Answers3

2

Try this

function findCompany($company){
    $query = $this->db->query("SELECT name FROM company WHERE name = '$company' ");
    $result = $query->result_array();

    if($result > 0)
    {
        return true;
    }else{
        return false;
    }
}

Error is

SELECT name FROM companyWHERE 
                       ^^^

And check wrapping " always ..

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
0

Try like this..

 function findCompany($company){
    $this->db->select('name');
    $this->db->where('name',$company);
    $query = $this->db->get('company');

    if($query->num_rows > 0) {
        return true;
    }else{
        return false;
    }
}
Hikmat Sijapati
  • 6,869
  • 1
  • 9
  • 19
0

Other than missing the quotes, you're also open for SQL injection. Instead of worrying about quotes, let Codeigniter/mysqli take care of it for you by passing it through as an argument.

$query = $this->db->query("SELECT name FROM company WHERE name = ?", array($company));
aynber
  • 22,380
  • 8
  • 50
  • 63