Reputation: 2752
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: (.*?) #i';
$patterns["country"] = '#Country: (.*?) #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;
}
?>
Upvotes: 0
Views: 294
Reputation: 249
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.
Upvotes: 0
Reputation: 1046
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'].") ")
Upvotes: 1