0

I am really stuck at this part.. I have tried and searched but my mind is exploding because I can't figure it out.

I found this piece of code on the net, and I want to store every array on MySQL separately. I need the code how to split this array and store each value in MySQL.

This is the output it has now: Array ( [domain] => dslb-094-219-040-096.pools.arcor-ip.net [country] => NL - Netherlands [state] => Saarland [town] => Schiffweiler )

But I want to store NL - Netherlands in e.g. MySQL table country.

Can someone please help me out?

Here is the code I found:

<?php
       $ip='94.219.40.96';
       print_r(geoCheckIP($ip));
       //Array ( [domain] => dslb-094-219-040-096.pools.arcor-ip.net [country] => DE - Germany [state] => Hessen [town] => Erzhausen )

       //Get an array with geoip-infodata
       function geoCheckIP($ip)
       {
               //check, if the provided ip is valid
               if(!filter_var($ip, FILTER_VALIDATE_IP))
               {
                       throw new InvalidArgumentException("IP is not valid");
               }

               //contact ip-server
               $response=@file_get_contents('http://www.netip.de/search?query='.$ip);
               if (empty($response))
               {
                       throw new InvalidArgumentException("Error contacting Geo-IP-Server");
               }

               //Array containing all regex-patterns necessary to extract ip-geoinfo from page
               $patterns=array();
               $patterns["domain"] = '#Domain: (.*?)&nbsp;#i';
               $patterns["country"] = '#Country: (.*?)&nbsp;#i';
               $patterns["state"] = '#State/Region: (.*?)<br#i';
               $patterns["town"] = '#City: (.*?)<br#i';

               //Array where results will be stored
               $ipInfo=array();

               //check response from ipserver for above patterns
               foreach ($patterns as $key => $pattern)
               {
                       //store the result in array
                       $ipInfo[$key] = preg_match($pattern,$response,$value) && !empty($value[1]) ? $value[1] : 'not found';
               }

               return $ipInfo;

       }

?>
2
  • so what exactly do you need, code how to insert rows into a mysql database? Commented May 22, 2013 at 23:28
  • Yes.. I need the code that splits the values and stores them into MySQL.. Commented May 22, 2013 at 23:30

2 Answers 2

1

you can save each part of the array separetely into a mysql database, that shouldn't be the problem.

you can check out this site on how to access mysql from within php: http://www.tizag.com/mysqlTutorial/mysqlinsert.php

and the sql insert query should help:

mysql_query("INSERT INTO country (country) VALUES(".$ipInfo['country'].") ") 
Sign up to request clarification or add additional context in comments.

1 Comment

I found out what the problem was that it did not store the values in the first place. I cannot print, echo or anything from outside of that function :/ Thanks
0

To me, the problem statement is a little vague. If it's just a single unnested array as cited in the example, then what Petros advised should work, when you add the missing apostrophes so that you'll have the following statement:

mysql_query("INSERT INTO country (country) VALUES('".$ipInfo['country']."') ") 

Note the added single-quote after the opening paren and before the closing paren. My preference when writing SQL statement within PHP code block is by using the heredoc format like the following so that I can see what the SQL statement should be (and eyeball it to see if it's missing something or has typos):

$sql =<<<raw
INSERT INTO country (country)
VALUES ('%s');
raw;
mysql_query(sprintf($sql, $ipInfo['country']));

It's longer, but it helps make the code more legible to me.

If your data is an array of arrays, then you'll want to to create an array of countries first, then use the implode commands to join all the values.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.