2015-11-19 02:56
浏览 420


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?


$con = mysql_connect("","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')";




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

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

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

 $ con = mysql_connect(“”,“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); 
 <  / DIV>
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongqiangou5724 2015-11-19 03:29
    • Error from your comment, after I suggested you check for errors on your query:

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '; VALUES('', '', '', '', '', '', '', '', '', '', '', '', '')' at line 1

    The error shows you where it starts right syntax to use near '; < right there.

    ... ptyds, ptavg); < see that semi-colon? Remove it. It's an end of statement character.

    However, you're doing foreach($data as $row) but not using $row.

    You need to change all $data['xxx'] to $row['xxx'] which is why your values are empty.

    If there are any characters that MySQL will complain about, then you will need to escape your data. Any which way, it's best that you do.

    As a bonus answer:

    Your present code is open to SQL injection. Use mysqli_* with prepared statements, or PDO with prepared statements.

    解决 无用
    打赏 举报

相关推荐 更多相似问题