我敢肯定这是一个简单的解决方案,但到目前为止我很难过......
因此,我尝试使用以下代码将数据插入到临时 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 并尝试像这样插入 $title :
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 Snake ( original / official ) [ As seen on Tosh.0 ]')' 附近使用的正确语法2"。现在,当我像这样在我的 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 之外的其他数据类型?
谢谢!