0
我想获取数据插入到指定的数据库,但它只是不会。我看了手动尝试的例子和所有这些,但我不能让数据传递到数据库,但我可以echo/print_r/var_dump它,所以我知道我有数据。这里是我的代码:INSERT INTO使用PDO不插入数据
public function insertJson($url, $subId)
{
$json_file = file_get_contents(KHAN_BASE.$url.'/videos');
if(isset($json_file))
{
$json_decoded = json_decode($json_file, true);
}else{
$this->error = 'A valid JSON file was not specified';
}
// var_dump($json_decoded); <--- This return all of the data needed from the json pull so i know I have data
//m3u8, mp4, png,
//". $row['m3u8'] .",". $row['mp4'] .",". $row['png'] .",
foreach($json_decoded as $row)
{
//echo $row['backup_timestamp'].'<br/>'; <--- This outputs the correct information so I know I can access it that way
$sql = "INSERT INTO tbl_khan_videos (sub_subject_id, backup_timestamp, date_added, description,
duration, extra_properties, has_questions, ka_url, keywords, kind, position, readable_id, relative_url, title, url, views,
youtube_id) VALUES (:subid, :backup_timestamp, :date_added, :description, :duration, :extra_properties, :has_questions, :ka_url, :keywords, :kind, :position,
:readable_id, :relative_url, :title, :url, :views, :youtube_id)";
$stmt = $this->db->prepare($sql);
$stmt->bindValue(":subid", $subId);
$stmt->bindValue(":backup_timestamp", $row['backup_timestamp']);
$stmt->bindValue(":date_added", $row['date_added']);
$stmt->bindValue(":description", $row['description']);
$stmt->bindValue(":duration", $row['duration']);
$stmt->bindValue(":extra_properties", $row['extra_properties']);
$stmt->bindValue(":has_questions", $row['has_questions']);
$stmt->bindValue(":ka_url", $row['ka_url']);
$stmt->bindValue(":keywords", $row['keywords']);
$stmt->bindValue(":kind", $row['kind']);
$stmt->bindValue(":position", $row['position']);
$stmt->bindValue(":readable_id", $row['readable_id']);
$stmt->bindValue(":relative_url", $row['relative_url']);
$stmt->bindValue(":title", $row['title']);
$stmt->bindValue(":url", $row['url']);
$stmt->bindValue(":views", $row['views']);
$stmt->bindValue(":youtube_id", $row['youtube_id']);
$stmt->execute();
}
}
林不知道我在做什么错。我已经尝试将它绑定为一个数组(例如:$array = array(':subId' => $subId); $stmt->execute($array);
),并且仍然没有收到数据到数据库。我知道我的配置$this->db
是好的,因为我可以使用其他形式的已经填充的数据。任何帮助将非常感激。
您是否尝试过使用insert语句手动进入'mysql'来检查语法或命名错误?你的PHP错误日志说什么?另外,我非常肯定你想在循环之前“准备”语句(出于效率的目的),但这不应该导致错误。 – Kurt 2012-08-04 05:08:33
我没有收到日志中的错误,我确实有登录。没有任何迹象表明MySQL语法是错误的,因为如果我围绕:placeholder添加引号,它将从PHPMyAdmin完全插入。也如代码中所述,我可以使用$ row ['name']设置回显一个变量,所以我知道我有信息存储在我的变量中。 – Cameeob2003 2012-08-04 05:27:36
几件事情。您无需每次在循环中准备语句!这样做会显着影响环路的速度。在进入循环之前准备好声明,并且每次重复使用它,这应该是预先准备的声明的好处之一。此外,您在代码的SQL部分中没有进行任何明显的错误检查。准备,绑定和执行所有发出错误或抛出异常(取决于模式)如果发生错误。如果发生这种情况,那么您需要检查errorCode和errorInfo出错的位置。 – GordonM 2012-08-04 05:55:32