PHP脚本成功地在MySQL中创建了表,但没有添加数据


PHP script successfully creating tables in MySQL but not adding data

我编写了一个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每个循环
尝试以下代码

$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
函数返回std对象类型,而不是数组。

所以使用

$game = $row->nfl_game_id;

而不是

$game = $row['nfl_game_id'];