1

I'm trying to get the IP out of the database and split them in different arrays so I can count the amount of clients in every country, and put them in a europe map chart.

Now I am stuck on the part where I split the countries into different arrays.

    $result_ip = $dbhandle->query("SELECT ip FROM email;");
    $row_cnt_ip = $result_ip->num_rows;

    $NL = array('');
    $AL = array('');
    $NO_EU = array('');


    $ip = $result_ip;
    $details = json_decode(file_get_contents("http://ipinfo.io/{$ip}"));

    while($r = $details->country->fetch_array(MYSQLI_ASSOC)):
       for($i = 1; $i < $row_cnt_ip; $i++) {
    switch ($details->country) {
        case "NL": //Netherlands
          array_push($NL,"$details->country");
            break;
        case "AL":  //Albania
            array_push($AL,"$details->country");
            break;
        default;
             array_push($NO_EU,"$details->country");
            break;
          }
         }
        endwhile;

The error I am getting is:

Catchable fatal error: Object of class mysqli_result could not be converted to string

Can anyone help me figuring out what the problem is?

IAmInPLS
  • 4,051
  • 4
  • 24
  • 57
Heis
  • 606
  • 5
  • 25
  • are you getting error in " echo $details->country;" ? if not then where you got this error , its like you are fething array and tri to print it with "echo" statement – Brijal Savaliya Apr 18 '16 at 07:41
  • try print_r($result_ip) and see what is in it – Amit Visodiya Apr 18 '16 at 07:42
  • @amit mysqli_result Object ( [current_field] => 0 [field_count] => 1 [lengths] => [num_rows] => 24 [type] => 0 ) – Heis Apr 18 '16 at 07:45
  • @BrijalSavaliya That was used as a test for myself removed it now – Heis Apr 18 '16 at 07:46
  • You are trying to fetch from `$details` but isn't `$ip` your mysql result? Can you `var_dump($details)`? – Paul Apr 18 '16 at 07:48
  • Is there any particular reason why you don't already have country info in the database? – Ignacio Vazquez-Abrams Apr 18 '16 at 07:49
  • fetch result from query result set "$result_ip" there are 24 records you need to get any one IP from record set and pass it to http://ipinfo.io/{$ip} – Amit Visodiya Apr 18 '16 at 07:52
  • @IgnacioVazquez-Abrams The database im using is an old one and already contains a lot of information, the moment it was made they didnt made it. made a copy of the database with only a few rows so i could see if the results we're true. – Heis Apr 18 '16 at 07:59

1 Answers1

1

You can't use mysqli_query result directly. You need to fetch data from your query result

$result_ip = $dbhandle->query("SELECT ip FROM email");
$row_cnt_ip = $result_ip->num_rows;
$NL = array('');
$AL = array('');
$NO_EU = array('');

$row=$result_ip->fetch_assoc();// fetch data 
$ip=$row['ip'];

$details = json_decode(file_get_contents("http://ipinfo.io/{$ip}"));
Saty
  • 22,443
  • 7
  • 33
  • 51
  • Fatal error: Call to a member function fetch_array() on line, its the line where i start the while loop ill look into it now. Thanks for the answer looks like im one step closer now. – Heis Apr 18 '16 at 08:04
  • Use `$result_ip->data_seek(0); while($r = $result_ip->fetch_array(MYSQLI_ASSOC)):` – Saty Apr 18 '16 at 08:34
  • @Satly Thanks for your suggestion, the errors are going but if i print the $NL its giving me 552 results but there is only 24 rows in the database – Heis Apr 18 '16 at 08:44
  • gave you the answer getting really close with this now only have to figure out why its given me 552 results instead of 24 – Heis Apr 19 '16 at 06:47