CodeIgniter Forums
Model query function not working with bound parameters - 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: Model query function not working with bound parameters (/showthread.php?tid=51639)



Model query function not working with bound parameters - El Forum - 05-11-2012

[eluser]zulubanslee[/eluser]
This function works if I hard code the values at the bottom of the query. I can confirm that the values are being passed via the parameters. But when I put in the question marks, it stops working. Am I making some foolish error?


Code:
function get_this_vehicle_info($ad_id, $user_id)
{

  $sql = "SELECT
      users.id AS user_id,
      ads.id AS ad_id,
      ads.year,
      ads.price,
      ads.mileage,
      ads.transmission,
      ads.description,
      ads.city,
      ads.state,
      ads.zip_code,
      ads.phone,
      vehicle_type.vehicle_type,
      vehicle_make.vehicle_make,
      vehicle_model.vehicle_model,
      vehicle_images.image_name
      FROM ads, vehicle_images, users, vehicle_type, vehicle_make, vehicle_model
      WHERE ads.vehicle_type = vehicle_type.id AND
      ads.vehicle_make = vehicle_make.id AND
      ads.vehicle_model = vehicle_model.id AND
      vehicle_images.fk_ad_id = ads.id AND
      ads.id   = ? AND
      users.id  = ?";

   //$query = $this->db->query($sql, array('ads.id', 'users.id'));
   $query = $this->db->query($sql, array('ad_id', 'user_id'));

   if($query->num_rows() > 0)
    return $query;
   else
    return FALSE;  
}



Model query function not working with bound parameters - El Forum - 05-11-2012

[eluser]zulubanslee[/eluser]
Incidentally, the query returns zero rows, not an error.


Model query function not working with bound parameters - El Forum - 05-11-2012

[eluser]CroNiX[/eluser]
See what it outputs.
Code:
echo $this->db->last_query();



Model query function not working with bound parameters - El Forum - 05-11-2012

[eluser]CroNiX[/eluser]
Shouldn't it be
Code:
$query = $this->db->query($sql, array($ad_id, $user_id));



Model query function not working with bound parameters - El Forum - 05-11-2012

[eluser]zulubanslee[/eluser]
Indeed that worked. I knew it was a stupid mistake.