Multiple DB Queries within a Controller using data pulled from a prior query

Permalink
Hi-

This seems like it would be so simple but yet it is not working. I am using outside tables in conjunction with Concrete5. I have sucessfully built a block that selects data from an outside table using data from the block table. I.e. when I add the block I enter data for hike_id. Based on the hike_id I then query an external table in the block controller and I am able to pull all the information from the outside table for the hike. Works great.

I now want to take a variable retrieved from the hike table (outside table) -- the region_code --- and query an outside region table to obtain the information on the region to include in the page. For some reason this does not work.

Here is a reduced version of the code:

$hike_key = trim($this->hike_id); (hike_id is from the block table)
$db = Loader::db();
$r = $db->query("SELECT * FROM HikingData WHERE hike_id = '{$hike_key}'");
while($row=$r->fetchrow()){
$this->set('hike_name' , $row['hike_name']);
$this->set('is_featured' , $row['is_featured']);
$this->set('short_desc' , $row['short_desc']);
$this->set('region_code', $row['region_code']);
etc....
}

$rs = $db->query("SELECT region_name FROM RegionData WHERE region_code = '{$region_code}'");
while($row=$rs->fetchrow()){
$this->set('region_name', $row['region_name']);
}

The second part of this -- retrieving the region name -- does not work. Is there some trick to using data retrieved in the controller for subsequent queries in the controller?

Thank you.

 
JohntheFish replied on at Permalink Best Answer Reply
JohntheFish
What it looks like you are missing between the 2 parts is
$region_code = $row['region_code'];

The 'set' only applies to data in the view and does not set anything in the controller.

As a general guide, its not good to insert data directly into a query and better to use arrays of parameters. This allows ADODB to quote, sanitize and optimize the query.
$row = $db->query['SELECT * FROM HikingData WHERE hike_id=?',array($hike_key));
dgreer replied on at Permalink Reply
Thank you. That did the trick.