Page 1 of 1

SQL query in a loop

Posted: Tue Sep 08, 2009 2:16 pm
by dkim777
Here is my PHP code what query mysql in a loop,
but I get same result everytime.
the counter is increasing, and query seem to work first time, but the rest of the loop run sql query will return same result.
any ideas?
Thanks

Code: Select all

 
<?php
 
        $ipaddress = $_POST['IP'];
        $ipaddress = nl2br($ipaddress);
        $ip_array = explode ("<br />", $ipaddress);
        $counter = 0;
 
        $db = mysql_connect($server, $username, $password) or die(mysql_error());
              mysql_select_db($dbname) or die(mysql_error());
 
        $sql = 'SELECT
                    c.country
                FROM
                    ip2nationCountries c,
                    ip2nation i
                WHERE
                    i.ip < INET_ATON("'.$ip_array[$counter].'") AND c.code = i.country
                ORDER BY
                    i.ip DESC
                LIMIT 0,1';
 
 
 
        echo "Your IP is ";
        echo $_SERVER['REMOTE_ADDR'];
        echo "<p>---------------------------------</p>";
 
        foreach ($ip_array as $search) {
                list($countryName) = mysql_fetch_row(mysql_query($sql));
                echo $countryName;
                echo " ----------";
                echo $sql;
                $counter++;
        }
?>
 

Re: SQL query in a loop

Posted: Wed Sep 09, 2009 12:52 pm
by dkim777
I did tried following, but same result.
any other ideas?

one thing I don't know how to do is getting PHP or mysql error log
where do I find the php or mysql errors in the code if there is one?
Thanks again.

Code: Select all

 
 foreach ($ip_array as $search) {
                 list($countryName) = mysql_fetch_row(mysql_query('SELECT
                                                                                         c.country
                                                                                        FROM
                                                                                      ip2nationCountries c,
                                                                                       ip2nation i
                                                                                         WHERE
                                                                                  i.ip < INET_ATON("'.$ip_array[$counter].'") AND c.code = i.country
                                                                                      ORDER BY
                                                                                        i.ip DESC
                                                                                     LIMIT 0,1'));
                 echo $countryName;
                 echo " ----------";
                 echo $sql;
                 $counter++;
         }
 

Re: SQL query in a loop

Posted: Thu Sep 10, 2009 2:38 pm
by dkim777
Found the problem
some how following code puts a blank space in front of every array except for the first one.
therefore only first one worked.. and I missed the space in the array entry all this time.
and I used "trim" to remove those blank spaces and it WORKS!

But your help caused me to look elsewhere for problems.
so Thank you very much!


# $ipaddress = $_POST['IP'];
# $ipaddress = nl2br($ipaddress);
# $ip_array = explode ("<br />", $ipaddress);