drkjzk3359 2011-08-31 04:11
浏览 46
已采纳

奇怪的数组值引起的MySQL语法错误应该不存在? [关闭]

This is really, really weird.

Basically, I have a MySQL syntax error occurring because of quotes in this string:

('Justin Bieber', '12312688'
 , 'http://a2.twimg.com/profile_images/1468693614/Inkwell_normal.jpeg', '0'),
('Nicki Minaj', '5422852'
 , 'http://a1.twimg.com/profile_images/1520039676/325789956_normal.jpg', '1'),
('Drizzy Drake', '2989144'
 ,'http://a0.twimg.com/profile_images/1483569177/drake-toronto_normal.jpg', '7'),
('Lil Wayne WEEZY F', '3058376'
 ,'http://a1.twimg.com/profile_images/712863751/lil-wayne-gq-2_normal.jpg', '6'),
('Kanye West', '4176631'
 ,'http://a3.twimg.com/profile_images/1132696610/securedownload_normal.jpeg', '4'),
('Wiz Khalifa', '2846894'
 ,'http://a3.twimg.com/profile_images/1400724773/5fa42d61-2b5e-4b12-ac9f-4e673c97ef16_8_normal.jpg', '8'),
('Beyonce Knowles', '1607181'
 ,'http://a1.twimg.com/profile_images/140949372/Beyonce_64_normal.jpg', '9'),
('KELENDRIA ROWLAND', '1214446'
 ,'http://a1.twimg.com/profile_images/1418081050/KELLY_ROWLAND_cover_8181v2_normal.jpg', '10'),
('Lupe Fiasco', '819343'
 ,'http://a2.twimg.com/profile_images/1450626759/209893_10150145920132282_8128837281_6973183_7422817_o_normal.jpg', '11'),
('Tinie Tempah', '761045'
 ,'http://a0.twimg.com/profile_images/1315150186/Tinie_Black_Jacket_Pic_normal.jpg', '13'),
('50cent', '4918067'
 ,'http://a3.twimg.com/profile_images/1508833809/street-king-energy_normal.png', '2'),
('TRINA ', '796672'
 ,'http://a1.twimg.com/profile_images/1441338027/Trina4372_normal.jpg', '12'),
('iamdiddy', '4012130'
 ,'http://a1.twimg.com/profile_images/1299211308/OG_FADER_FORT_DAY_4_19_normal.jpg', '5'),
('mediatemple', 
 '('Justin Bieber', '12312688'
   ,'http://a2.twimg.com/profile_images/1468693614/Inkwell_normal.jpeg', '0')'
   ,'http://a2.twimg.com/profile_images/421686554/mt-125x125-dk__d5d6295_normal.jpg', '14'),
  ('Chris Brown ', '4284795'
   ,'http://a2.twimg.com/profile_images/1515163027/image_normal.jpg', '3')

More specifically this portion, of which the Justin Bieber related part shouldn't be there:

('mediatemple', 
 '('Justin Bieber', '12312688'
   ,'http://a2.twimg.com/profile_images/1468693614/Inkwell_normal.jpeg', '0')'
   ,'http://a2.twimg.com/profile_images/421686554/mt-125x125-dk__d5d6295_normal.jpg', '14')

Here's my actual code:

$screennames = array(
                 0 => "JustinBieber",
                 1 => "NickiMinaj",
                 2 => "drakkardnoir",
                 3 => "LilTunechi",
                 4 => "kanyewest",
                 5 => "RealWizKhalifa",
                 6 => "beyonce",
                 7 => "KELLYROWLAND",
                 8 => "LupeFiasco",
                 9 => "TinieTempah",
                 10 => "50cent",
                 11 => "TRINArockstarr",
                 12 => "iamdiddy",
                 13 => "mediatemple",
                 14 => "chrisbrown"
                     );

for($i = 0; $i < 15; $i++) {
$xml[$i] = @simplexml_load_file('http://api.twitter.com/1/users/show.xml?screen_name=' . $screennames[$i]);
$name[$i] = $xml[$i]->name;
$followers[$i] = $xml[$i]->followers_count;
$imageurl[$i] = $xml[$i]->profile_image_url;
}

$rank=-1;

arsort($followers, SORT_NUMERIC); 
foreach ($followers as $key=>$value) {
    $rank++;
  $ranks[$key]=$rank;
}

for($i = 0; $i < 15; $i++) {
    $value[$i] = "('".$name[$i]."', '".$followers[$i]."', '".$imageurl[$i]."', '".$ranks[$i]."')";
}
for($i = 0; $i < 14; $i++) {
    $value_string .= $value[$i].",";
}

$value_string .=$value[14];

For reference, it queries the Twitter API, retrieves specific data about each $screenname value, then concatenates it into one big string for (...) VALUES $value_string, however on $value[13] it seems to just place $value[1] right in the middle of it.

So far I've tried;

Using implode (which always returns a function error).

Just using $value_string .=$value[$i]."," in the first for loop which leaves a trailing ,.

Changing $screennames values around.

I just don't understand what could be causing this issue.

Any help/answers/comments would be very, very, very much appreciated :)!!

UPDATE 1

Here's the final portion of my code, for good measure:

include("db_conn.php");
$conn = mysql_connect($db_host, $db_user, $db_pass) or die(mysql_error()); mysql_select_db($db_name) or die(mysql_error());

$query = "INSERT INTO twitter (name, followers, imageurl, rank) VALUES $value_string";
$result = mysql_query($query) or die(mysql_error());
mysql_close($conn);
  • 写回答

3条回答 默认 最新

  • dongshuo6185 2011-08-31 04:45
    关注

    fixed:

    replace from first for loop down

    for($i = 0; $i < 15; $i++) {
       $value_string .="('".$name[$i]."', '".$followers[$i]."', '".$imageurl[$i]."', '".$ranks[$i]."'),";
    }
    
    $value_string=rtrim($value_string,",");
    echo $value_string;
    

    also removes the 2nd for loop which is rather pointless.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100