Php 将JSON数组发布到MySQL
我正在尝试为学校作业创建一个PHP商店定位器。我是个彻头彻尾的傻瓜,所以请原谅我犯了明显的错误。我正在尝试从bbyopen API获取门店信息,并创建一个网页,允许我输入zipcode并填充最近的门店位置。我有以下生成JSON数组的代码,但我无法在MySQL中获取数据。连接到数据库但不发布。非常感谢您的帮助 我的php代码Php 将JSON数组发布到MySQL,php,mysql,arrays,json,google-maps,Php,Mysql,Arrays,Json,Google Maps,我正在尝试为学校作业创建一个PHP商店定位器。我是个彻头彻尾的傻瓜,所以请原谅我犯了明显的错误。我正在尝试从bbyopen API获取门店信息,并创建一个网页,允许我输入zipcode并填充最近的门店位置。我有以下生成JSON数组的代码,但我无法在MySQL中获取数据。连接到数据库但不发布。非常感谢您的帮助 我的php代码 <?php $json = file_get_contents('http://api.remix.bestbuy.com/v1/stores?show=storeI
<?php
$json = file_get_contents('http://api.remix.bestbuy.com/v1/stores?show=storeId,name,address,city,postalCode,phone,lat,lng&format=json&apiKey={apiKey}');
$data = json_decode($json,true);
$stores = $data['stores'];
echo "<pre>";
print_r($stores);
$stores1 = array(
'storeId' => $json->storeId,
'name' => $json->name,
'address' => $json->address,
'city' => $json->city,
'postalCode' => $json->postalCode,
'phone' => $json->phone,
'lat' => $json->lat,
'lng' => $json->lng,
);
$stores2 = '"'.implode(array(
$stores1['storeId'],
$stores1['name'],
$stores1['address'],
$stores1['city'],
$stores1['postalCode'],
$stores1['phone'],
$stores1['lat'],
$stores1['lng'],
),'","').'"';
$username = "root";
$password = "root";
$hostname = "127.0.0.1:8889";
//connection to the eventbase
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
//select a eventbase to work with
$selected = mysql_select_db("bbyopen",$dbhandle)
or die("Could not select bbyopen");
// Insert $event array into eventbase
$db_insert = mysql_query("INSERT INTO markers (storeId, name, address, city, postalCode, phone, lat, lng)
VALUES ($stores2)");
if (!$db_insert)
{
die('Could not connect - event insert failed: ' . mysql_error());
}
?>
像这样修改代码
$json=file\u get\u contents('http://api.remix.bestbuy.com/v1/stores?show=storeId,姓名,地址,城市,邮政编码,电话,lat,lng&format=json&apiKey={apiKey}');
$data=json_decode($json,true);
$stores=$data['stores'];
回声“;
印刷品(商店);
$values=array();
$i=0;
foreach($stores作为$store存储){
$line=“(”;
foreach($存储为$key=>$value){
$line=$line.“.”“$value.“,”;
}
$line=substr($line,0,strlen($line)-1)。”;
$values[$i]=$line;
++$i;
}
$values=内爆(“,”,$values);
回声$值;
$username=“root”;
$password=“123qwe!@#”;
$hostname=“127.0.0.1”;
//与eventbase的连接
$dbhandle=mysql\u connect($hostname、$username、$password)
或者死亡(“无法连接到MySQL”);
echo“连接到MySQL
”;
//选择要使用的事件库
$selected=mysql\u select\u db(“测试”,$dbhandle)
或死亡(“无法选择bbyopen”);
//将$event数组插入eventbase
$db_insert=mysql_query(“插入标记(城市、地址、姓名、液化天然气、邮政编码、电话、存储ID、lat)值”。$VALUES);
如果(!$db\u插入)
{
die('无法连接-事件插入失败:'.mysql_error());
}
请注意,我不知道您的表结构。假设所有表字段都键入varchar(255)。您需要学习如何编写sql语句。我希望这个例子能对您有所帮助。效果很好。非常感谢。我一定会学习sql语句。
Array
)
[0] => Array
(
[city] => San Juan
[address] => 230 Calle Federico Costa Hato Rey
[name] => Hato Rey
[lng] => -66.07188
[postalCode] => 00918
[phone] => 787-764-4900
[storeId] => 1118
[lat] => 18.42684
)
[1] => Array
(
[city] => Bayamon
[address] => 60 Ave Rio Hondo Ste 60
[name] => Rio Hondo
[lng] => -66.16224
[postalCode] => 00961
[phone] => 787-522-0999
[storeId] => 1090
[lat] => 18.42217
)
[2] => Array
(
[city] => Plaza Carolina
[address] => Villa Fontana Ave Fragoso
[name] => Plaza Carolina
[lng] => -65.9688829
[postalCode] => 00983
[phone] => 787-522-4549
[storeId] => 1496
[lat] => 18.3912915
)
[3] => Array
(
[city] => Hadley
[address] => 367 Russell St
[name] => Hadley
[lng] => -72.54847
[postalCode] => 01035
[phone] => 800-375-1736
[storeId] => 683
[lat] => 42.357971
)
[4] => Array
(
[city] => Holyoke
[address] => 50 Holyoke St
[name] => Holyoke
[lng] => -72.643005
[postalCode] => 01040
[phone] => 413-533-4443
[storeId] => 418
[lat] => 42.169613
)
[5] => Array
(
[city] => Holyoke
[address] => #B216, 50 Holyoke Street, Ingleside mall
[name] => Best Buy Mobile - Holyoke Mall
[lng] => -72.640241
[postalCode] => 01040
[phone] => 413-535-2070
[storeId] => 2843
[lat] => 42.17108
)
[6] => Array
(
[city] => Lanesboro
[address] => 655 Cheshire Rd
[name] => Pittsfield
[lng] => -73.205688
[postalCode] => 01237
[phone] => 413-445-5812
[storeId] => 548
[lat] => 42.493664
)
[7] => Array
(
[city] => Leominster
[address] => 33 Orchard Hill Park Dr
[name] => Leominster
[lng] => -71.712425
[postalCode] => 01453
[phone] => 978-537-9042
[storeId] => 1433
[lat] => 42.527382
)
[8] => Array
(
[city] => Auburn
[address] => 385 Southbridge Street, S080
[name] => Best Buy Mobile - Auburn Mall
[lng] => -71.835952
[postalCode] => 01501
[phone] => 508-832-3203
[storeId] => 2901
[lat] => 42.203384
)
[9] => Array
(
[city] => Millbury
[address] => 70 Worcester Providence Turnpike
[name] => Millbury
[lng] => -71.77605
[postalCode] => 01527
[phone] => 508-421-9149
[storeId] => 2506
[lat] => 42.19519
)
)
Connected to MySQL
$json = file_get_contents('http://api.remix.bestbuy.com/v1/stores?show=storeId,name,address,city,postalCode,phone,lat,lng&format=json&apiKey={apiKey}');
$data = json_decode($json,true);
$stores = $data['stores'];
echo "<pre>";
print_r($stores);
$values=array();
$i = 0;
foreach($stores as $store){
$line="(";
foreach($store as $key => $value){
$line = $line. "'". $value . "',";
}
$line = substr($line, 0, strlen($line)-1).")";
$values[$i] = $line;
++$i;
}
$values = implode(",", $values);
echo $values;
$username = "root";
$password = "123qwe!@#";
$hostname = "127.0.0.1";
//connection to the eventbase
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
//select a eventbase to work with
$selected = mysql_select_db("test",$dbhandle)
or die("Could not select bbyopen");
// Insert $event array into eventbase
$db_insert = mysql_query("INSERT INTO markers (city, address, name, lng, postalCode, phone, storeId, lat) VALUES" . $values);
if (!$db_insert)
{
die('Could not connect - event insert failed: ' . mysql_error());
}