我敢肯定,這是一個簡單的解決方案,但到目前爲止,我很爲難......錯誤插入的字符串到MySQL臨時表
所以我嘗試使用下面的代碼將數據插入到一個臨時MySQL表:
mysql_query("CREATE TEMPORARY TABLE IF NOT EXISTS data(
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
name VARCHAR(255))")
or die(mysql_error());
$search_term = 'snakes';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://gdata.youtube.com/feeds
/api/videos?q='.$search_term.'&safeSearch=none&orderby=viewCount&v=2&alt=json&start-
index=1&max-results=20');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
curl_close($ch);
$data = json_decode($output,true);
$info = $data["feed"];
$video = $info["entry"];
$nVideo = count($video);
foreach ($video as $video) {
$video_id = $video['media$group']['yt$videoid']['$t'];
$title = $video['title']['$t'];
$insert = "INSERT INTO data (name) " .
"VALUES
('$video_id')";
$results = mysql_query($insert)
or die(mysql_error());
}
當我離開這段代碼($ video_id作爲插入到表中的值)時,一切都很完美。但是,如果我改變$ VIDEO_ID並試圖插入$標題,而不是像這樣:
foreach ($video as $video) {
$video_id = $video['media$group']['yt$videoid']['$t'];
$title = $video['title']['$t'];
echo $title;
$insert = "INSERT INTO data (name) " .
"VALUES
('$title')";
$results = mysql_query($insert)
or die(mysql_error());
}
我收到「您的SQL語法錯誤;檢查對應於您的MySQL服務器版本正確的語法手冊在'ma蛇(原來的/官方)[在Tosh.0]')'在'第二行看到'使用「。現在,當我回聲出值,我的foreach循環類似這樣的標題內....
foreach ($video as $video) {
$video_id = $video['media$group']['yt$videoid']['$t'];
$title = $video['title']['$t'];
echo $title;
echo "<br>";
$insert = "INSERT INTO data (name) " .
"VALUES
('$video_id')";
$results = mysql_query($insert)
or die(mysql_error());
}
我得到:
GIANT SNAKE EATS SECURITY GUARD
Snake woman
Biggest Snake of the World for Sale, 25 000 €
Giant Snake
A Huge Centipede Fighting A Snake
Scary Killer Snakes, World Biggest Snake Ever!
Five Headed Snake In India.
Clelia eats Bothrops
snake swallowed a hippo
The world biggest Snake has been found in SAAD - Karaj (Iran)
WORLD LARGEST SNAKE FOUND DEAD! ★★★★★
Cobra vs. Rat Snake
I'm a Snake (original/official) [ As seen on Tosh.0 ]
Bull Snake Against Squirrel
The Biggest Snake In The World
Shark Vs. Sea Snake
Killer Karaoke - Karaoke Singer Gets Dunked in a Tank of Snakes
This is What Snake Venom Does to Blood!
Snake befriends its hamster lunch in zoo
ZOMBIE SNAKE HEAD STILL ALIVE!
我知道這些值與一些奇怪的字符的字符串,所以我的問題是...是我的語法真的錯誤,如錯誤消息暗示或我需要使用VARCHAR以外的其他數據類型?
謝謝!
有兩點需要注意,因爲你是新來的網站:#1請確保您標記的答案是正確的/ upvoted一旦幫了你! #2你應該刪除與你手邊的問題無關的部分代碼。代碼很好 - 不相關的代碼很糟糕。 – slifty