Another questions related to the use of variables in loops. I have two mysql databases with the following columns:
country_db (that's the name of the db) country_name country_cit_rate country_level
company_db (that's the name of the db) company_name company_country company_level
I want to make a number of calculations for each company (defined by "company_name") and these calculations should run from the highest level ("company_level") to the company_level with the number 1.
Therefore I created the following loop ($level_max is previously defined as the highest company_level in the company_db):
do
{
echo "The number is: $level_max <br>";
$company_variables_array = mysqli_query($con,"SELECT company_name, company_country FROM company_db WHERE company_level=$level_max");
while($company_variables_fetch = mysqli_fetch_array($company_variables_array))
{
$company_name = $company_variables_fetch['company_name'];
$company_country = $company_variables_fetch['company_country'];
echo $company_name;
echo $company_country;
}
$level_max--;
}
while ($level_max>0);
By using the echo statements I can see that the code works exactly as I want it.
However, I've been struggling two days with the next step. For each company IN THE LOOP I would like to obtain the related value in the column "country_cit_rate" of the "country_db". Since $company_country has been defined within the loop I expected this to be an easy step but I get the following error message: "Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in XXX on line XXX"
I used to following code:
do
{
echo "The number is: $level_max <br>";
$company_variables_array = mysqli_query($con,"SELECT company_name, company_country FROM company_db WHERE company_level=$level_max");
while($company_variables_fetch = mysqli_fetch_array($company_variables_array))
{
$company_name = $company_variables_fetch['company_name'];
$company_country = $company_variables_fetch['company_country'];
echo $company_name;
echo $company_country;
$company_country_cit_rate_array = mysqli_query($con,"SELECT country_cit_rate FROM country_db WHERE country_name=$company_country");
$company_country_cit_rate_fetch = mysqli_fetch_array($company_country_cit_rate_array);
$company_country_cit_rate = $company_country_cit_rate_fetch['country_cit_rate'];
echo $company_country_cit_rate . "%";
}
$level_max--;
}
while ($level_max>0);
What am I doing wrong?