0

I have already researched on this but didnt come to get the correct answer, I am trying to pass multiple arguments to an sql query in code igniter without using active records and its not working for me, see what I have done below

in my model I have:

function get_values($id, $age)
{
$sql='SELECT * FROM tblRegister where id=? AND unit=?';
$query=$this->db->query($sql, array('$id','$age'));
return $query->result_array();

}

in the controller I have:

function get_values()
{
$result=$this->register_model->get_values(32, 23);

}

this doesnt work for me, i think its an error with passing the arguments to the query, how to I format the syntax so that it works just fine? Regards

Mike Aono
  • 859
  • 2
  • 11
  • 17
  • 2
    You don't need the single quotes around variables: `$query=$this->db->query($sql, array($id, $age));` – Eggplant Jul 19 '13 at 09:18
  • how come i works with the quotes when am passing just one argument? say for example: if I only needed to pass the id i would be $this->db->query($sql, array('$id')); – Mike Aono Jul 19 '13 at 09:23
  • First, at this point you should really define what "it works" and "it doesn't work" mean for you, please provide some details about that. Second, if you use single quotes you are actually passing an array composed of two strings `0=>'$id', 1 =>'$age'`, and that query is probably expecting some numerical values, isn't it? You said another query works if you use the quotes, well that query probabaly accepts a string parameter. Third, have you tried if it works removing the quotes? – Eggplant Jul 19 '13 at 09:27
  • sorry for my terminologies but by works i mean runs, and yes it runs without the quotes, and the suggestion of indexing the array still hasn't had it solved am still – Mike Aono Jul 19 '13 at 09:52
  • There's no need to index the array, that was just to show you what you are actually passing in the array, which is wrong. All you need to do is removing those single quotes as I wrote in the first comment, and it should be executed fine: `$query=$this->db->query($sql, array($id, $age));` – Eggplant Jul 19 '13 at 09:56

4 Answers4

8

Try variables without quotation marks: array($id, $age)

function get_values($id, $age)
{
   $sql='SELECT * FROM tblRegister where id=? AND unit=?';
   $query=$this->db->query($sql, array($id,$age));
   return $query->result_array();
}
Bora
  • 10,529
  • 5
  • 43
  • 73
2
//do it as:
function get_values($id, $age)
{     
   $this->db->where("id",$id);
   $this->db->where("unit",$age);
   $query=$this->db->get("tblRegister");
   return $query->result_array();

}
  • this one works well but I am trying to achieve this without using active records, my aim is to be able to pass the arguments to the query – Mike Aono Jul 19 '13 at 09:40
  • 3
    Mike you are showing no respect here, really! I wrote you quite a descriptive comment 15 minutes ago, which you just decided to ignore, and now you ask the same thing again? Look, I've explained you the reason why your code *doesn't work*, have you tries to remove those single quotes? How can one help you if you don't even try what he suggests you? Remove those single quotes and then give us some feedback! – Eggplant Jul 19 '13 at 09:49
0
// try this in your model
function get_values($id, $age)
{    
    $array = array('id' => $id, 'unit' => $age);

    $this->db->select('*');
    $this->db->from('tblRegister');
    $this->db->where($array);
    $query=$this->db->get();

    return $query->result_array();
}
user2351410
  • 91
  • 1
  • 3
0

TRY THIS

function get_values($id, $age)
{
    $this->db->where('id',$id);

   $this->db->where('age',$age);

   $query=$this->db->get('tblRegister');

   return $query->result_array();
}
Harshad Hirapara
  • 462
  • 3
  • 12