Welcome Guest, Not a member yet? Register   Sign In
Query returning different results in phpMyAdmin and CodeIgniter
#1

[eluser]Uriptical[/eluser]
Hi all,

In my model I have following function:

Code:
public function do_region_and_country_match($region_name, $country_name)
  {      
    $query = "
SELECT COUNT( timezone_countries.name ) AS countries_in_region
FROM timezone_countries
JOIN timezone_regions ON timezone_regions.id = timezone_countries.timezone_region_id
WHERE (
timezone_regions.name =  '" . $region_name . "'
)
AND (
timezone_countries.name =  '" . $country_name . "'
)";

    echo "the query is: " . $query . "<br /><br />";
    
    $result_set = $this->db->query($query);
    
    echo "number of rows = " . $result_set->num_rows() . "<br />";  
        
    foreach ($result_set->result() as $row)
    {
      if ($row->countries_in_region = 1) {
        echo "countries_in_region = " . $row->countries_in_region . "</b>. region = " . $region_name . ". country = " . $country_name . "<br />";
        return true;
      }
      else {
        echo "countries_in_region = " . $row->countries_in_region . "</b>. region = " . $region_name . ". country = " . $country_name . "<br />";
        return false;
      }
    }      
  }

It returns the following output:

Quote:the query is: SELECT COUNT( timezone_countries.name ) AS countries_in_region FROM timezone_countries JOIN timezone_regions ON timezone_regions.id = timezone_countries.timezone_region_id WHERE ( timezone_regions.name = 'Africa' ) AND ( timezone_countries.name = '' )

number of rows = 1
countries_in_region = 1. region = Africa. country =

The result, that being that the result is 1, is not correct. The correct result, however, is shown when I run the following query in phpMyAdmin:

Code:
SELECT COUNT( timezone_countries.name ) AS countries_in_region FROM timezone_countries JOIN timezone_regions ON timezone_regions.id = timezone_countries.timezone_region_id WHERE ( timezone_regions.name = 'Africa' ) AND ( timezone_countries.name = '' )

The result in phpMyAdmin is:

Quote:countries_in_region
0

Any ideas on what I've done wrong with the php code to get a result of 1 would be much appreciated.
#2

[eluser]m4rw3r[/eluser]
Ummm, you assign a 1 to $row->countries_in_region, I believe you want to use == .
#3

[eluser]Uriptical[/eluser]
Hmm, that was a rather obvious mistake. I've been writing too much Delphi code or at least that's my excuse. Wink

Thanks for your help.

It's interesting that values can be assigned straight after an IF statement where a comparison between two values is normally made. I'm curious to know if there would be any coding situations where this feature would be used.
#4

[eluser]m4rw3r[/eluser]
Code:
if($node = $this->MPTtree->xpath(array('about','me'))){
    // do something with $node, it was not false
}
else{
    // $node was false (or zero, PHP does not make a difference), do something else
}
The assignment occurs first, then a comparison is made (in this case, the value of $node.
Or, you could write it like this:
Code:
$node = $this->MPTtree->xpath(array('about','me'));
if($node]{
    // ...




Theme © iAndrew 2016 - Forum software by © MyBB