dsa89029
2015-11-19 02:56
浏览 435
已采纳

JSON数组到MySQL数据库

I'm working on a project that involves a PHP script that calls an API and gets a JSON array. I then want to put this JSON array into a MySql database. The issue I am running into is that while the script executes without any errors or exceptions in the terminal, my database is not filling with any data.

I am running MySQL Workbench as my MySQL client and have created a schema called "team_data" into which I am attempting to input my JSON array. I have removed my API key for obvious reasons. Any ideas where I am going wrong here?

<?php

$con = mysql_connect("127.0.0.1","XXXXXX","XXXXXX") or die('Could not connect: ' . mysql_error());
    mysql_select_db("test1", $con);

$json = file_get_contents('team_data.json');    
$data = json_decode($json, true);

foreach($data as $row)
{

    $game = $data['nfl_game_id'];
    $team = $data['team'];
    $opponent = $data['opponent'];
    $totfirstdown = $data['totalfirstdown'];
    $totyds = $data['totyds'];
    $pyds = $data['pyds'];
    $ryds = $data['ryds'];
    $pen = $data['pen'];
    $penyds = $data['penyds'];
    $trnovr = $data['trnovr'];
    $pt = $data['pt'];
    $ptyds = $data['ptyds'];
    $ptavg = $data['ptavg'];

    $sql = "INSERT INTO Teams(nfl_game_id, team, opponent, totalfd, totyds, pyds, ryds, pen, penyds, trnovr, pt, ptyds, ptavg);
    VALUES('$game', '$team', '$opponent', '$totfirstdown', '$totyds', '$pyds', '$ryds', '$pen', '$penyds', '$trnovr', '$pt', '$ptyds', '$ptavg')";

    mysql_query($sql,$con);

}



?>

图片转代码服务由CSDN问答提供 功能建议

我正在开发一个涉及调用API并获取JSON数组的PHP脚本的项目。 然后我想把这个JSON数组放到MySql数据库中。 我遇到的问题是,当脚本在终端中执行时没有任何错误或异常时,我的数据库没有填充任何数据。 </ p>

我正在运行MySQL Workbench作为我的MySQL客户端,并创建了一个名为“team_data”的模式,我试图输入我的JSON数组。 我出于显而易见的原因删除了我的API密钥。 我在这里出错的任何想法?</ p>

 &lt;?php 
 
 $ con = mysql_connect(“127.0.0.1”,“XXXXXX”,“XXXXXX”) 或者死('无法连接:'。mysql_error()); 
 mysql_select_db(“test1”,$ con); 
 
 $ json = file_get_contents('team_data.json');  
 $ data = json_decode($ json,true); 
 
foreach($ data as $ row)
 {
 
 $ game = $ data ['nfl_game_id']; 
 $ team = $ data [  'team']; 
 $ opponent = $ data ['opponent']; 
 $ totfirstdown = $ data ['totalfirstdown']; 
 $ totyds = $ data ['totyds']; 
 $ pyds = $  data ['pyds']; 
 $ ryds = $ data ['ryds']; 
 $ pen = $ data ['pen']; 
 $ penyds = $ data ['penyds']; 
 $ trnovr  = $ data ['trnovr']; 
 $ pt = $ data ['pt']; 
 $ ptyds = $ data ['ptyds']; 
 $ ptavg = $ data ['ptavg']; 
  
 $ sql =“INSERT INTO Teams(nfl_game_id,team,opponent,totalfd,totyds,pyds,ryds,pen,penyds,trnovr,pt,ptyds,ptavg); 
 VALUES('$ game','$ team'  ,'$ opponent','$ totfirstdown','$ totyds','$ pyds','$ ryds','$ pen','$ penyds','$ trnovr','$ pt','$ ptyds'  ,'$ ptavg')“; 
 
 mysql_query($ sql,$ con); 
 
} 
 
 
 
?&gt; 
 </ code> </ pre> 
 <  / DIV>

2条回答 默认 最新

相关推荐 更多相似问题