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