Note that there are some explanatory texts on larger screens.

plurals
  1. POPDO: Calculate a value using data from two different tables
    primarykey
    data
    text
    <p>What I'm trying to do is calculate the total value of land that a company owns for an online game. The current value of the land for each city is stored in the city_list table as acre_price. The amount of acres that a company owns is stored in the company_land table as amount. So the total value would be acre_price * amount, and do that for every city that the company owns land in, and then add it all up. </p> <p>The reason why acre_price can't be stored in the company_land table is because it is a constantly changing value - it can change every few minutes if there is enough activity, and keeping the land value up to date in two different tables would be a lot of work. </p> <p>This is what I have come up with, and it halfway works. It calculates the correct value, but only for 1 city that a company owns land in (the most recent entry in the database). I need it to calculate the value for ALL entries in the database. I've tried changing fetch to fetchAll in the 2nd query, but that broke the code and only displayed a white page. </p> <p>Does anyone know what I would need to do to achieve this? </p> <p>Thanks in advance :)</p> <pre><code>$sth = null; $count = 0; $data = array(); $sth = $dbh-&gt;prepare("SELECT * FROM company_land WHERE company_id = ?"); $company_land_query = $sth-&gt;execute(array($_GET['id'])); if($company_land_query){ while($row = $sth-&gt;fetch()){ $land_data = $row; ++$count; } if($count &gt; 0){ $sth = null; $count = 0; $data = array(); $sth = $dbh-&gt;prepare("SELECT * FROM city_list WHERE city_id = ?"); $land_price_query = $sth-&gt;execute(array($land_data['city_id'])); while($row = $sth-&gt;fetch()){ $city_data = $row; $land_value = $land_data['amount'] * $city_data['acre_price']; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload