我编写了一个 php 循环来解析 JSON 数据并创建它并将其输入到 MySQL 表中。问题是表已成功创建,我没有收到任何我构建的错误消息,尤其是关于无法将 JSON 数据添加到表中的错误消息(这显然是问题所在)。知道是什么阻止脚本将数据输入 MySQL 吗?
// Create Table to Hold JSON
$sql = "CREATE TABLE {$value}_GAMES ( ". // Creating a new table for each team
"nfl_game_id INT NOT NULL, ".
"team VARCHAR(3) NOT NULL, ".
"opponent VARCHAR(3) NOT NULL, ".
"totfd INT NOT NULL, ".
"totyds INT NOT NULL, ".
"pyds INT NOT NULL, ".
"ryds INT NOT NULL, ".
"pen INT NOT NULL, ".
"penyds INT NOT NULL, ".
"trnovr INT NOT NULL, ".
"pt INT NOT NULL, ".
"ptyds INT NOT NULL, ".
"ptavg INT NOT NULL, ".
"PRIMARY KEY ( nfl_game_id ));";
$retval = mysql_query($sql, $con); // Execute SQL Code
if(! $retval)
{
die('Could not create table: ' . mysql_error());
}
echo 'Table created successfully\n';
// Process JSON Data
$data = json_decode($result, true); // Decode JSON
foreach($data as $row)
{
$game = $row['nfl_game_id'];
$team = $row['team'];
$opponent = $row['opponent'];
$totfirstdown =$row['totfd'];
$totyds = $row['totyds'];
$pyds = $row['pyds'];
$ryds = $row['ryds'];
$pen = $row['pen'];
$penyds = $row['penyds'];
$trnovr = $row['trnovr'];
$pt = $row['pt'];
$ptyds = $row['ptyds'];
$ptavg = $row['ptavg'];
// Insert data into team-specific table
$sql = "INSERT INTO {$value}_GAMES (nfl_game_id, team, opponent, totfd, ".
"totyds, pyds, ryds, pen, penyds, trnovr, pt, ptyds, ptavg)".
"VALUES('$game', '$team', '$opponent', '$totfirstdown', '$totyds',".
"'$pyds', '$ryds', '$pen', '$penyds', '$trnovr', '$pt', '$ptyds', ".
"'$ptavg')";
$ret_val = mysql_query($sql,$con); // Execute SQL Code
if(! $ret_val)
{
die('Could not add JSON data to table: ' . mysql_error());
}
}
mysql_close($con); // Close Connection
}
最佳答案
试试这个对我有用
你必须使用 $retval 而不是 $result
在查询之前关闭 for each 循环
试试下面的代码
$sql = "CREATE TABLE GAMES ( ".
"nfl_game_id INT NOT NULL, ".
"team VARCHAR(3) NOT NULL, ".
"opponent VARCHAR(3) NOT NULL, ".
"totfd INT NOT NULL, ".
"totyds INT NOT NULL, ".
"pyds INT NOT NULL, ".
"ryds INT NOT NULL, ".
"pen INT NOT NULL, ".
"penyds INT NOT NULL, ".
"trnovr INT NOT NULL, ".
"pt INT NOT NULL, ".
"ptyds INT NOT NULL, ".
"ptavg INT NOT NULL, ".
"PRIMARY KEY ( nfl_game_id ));";
$retval = mysql_query($sql, $conn); // Execute SQL Code
if(! $retval)
{
die('Could not create table: ' . mysql_error());
}
echo 'Table created successfully\n';
// Process JSON Data
$data = json_decode($retval, true); //You have to use $retval instead of $result
foreach($data as $row)
{
$game = $row['nfl_game_id'];
$team = $row['team'];
$opponent = $row['opponent'];
$totfirstdown =$row['totfd'];
$totyds = $row['totyds'];
$pyds = $row['pyds'];
$ryds = $row['ryds'];
$pen = $row['pen'];
$penyds = $row['penyds'];
$trnovr = $row['trnovr'];
$pt = $row['pt'];
$ptyds = $row['ptyds'];
$ptavg = $row['ptavg'];
} //close your for each loop over here
// Insert data into team-specific table
$sql = "INSERT INTO GAMES (nfl_game_id, team, opponent, totfd, ".
"totyds, pyds, ryds, pen, penyds, trnovr, pt, ptyds, ptavg)".
"VALUES('$game', '$team', '$opponent', '$totfirstdown', '$totyds',".
"'$pyds', '$ryds', '$pen', '$penyds', '$trnovr', '$pt', '$ptyds', ".
"'$ptavg')";
$ret_val = mysql_query($sql,$conn); // Execute SQL Code
if(! $ret_val)
{
die('Could not add JSON data to table: ' . mysql_error());
}
mysql_close($conn); // Close Connection
关于PHP 脚本在 MySQL 中成功创建表但未添加数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33951991/