开发者

MySQL syntax error caused by weird array value which shouldn't be there? [closed]

开发者 https://www.devze.com 2023-04-01 02:39 出处:网络
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time,or an extraordinarily narrow situation that is not generally applic
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 9 years ago.

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开发者_如何学C",
                 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);


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.


The problem disappear for me if I declare $value as an array before using it:

$value = array();   // <-- here
for($i = 0; $i < 15; $i++) {

    $value[$i] = "('".$name[$i]."', '".$followers[$i]."', '".$imageurl[$i]."', '".$ranks[$i]."')";

}

Explanation: I think the problem is that $value is used before as a scalar variable first in the foreach ($followers segment, then as an array afterward. weird combination

@Pixelatron. If you want to bypass the rate limit, just cache the results in a temp file for testing..

$xml[$i] = @simplexml_load_file('http://api.twitter.com/1/users/show.xml?screen_name=' . $screennames[$i]);
file_put_contents("temp/".$screennames[$i],$xml[$i]->asXML());

and then comment that off, and load the cache instead of the loading the remote xml

$xml[$i] = file_get_contents("temp/".$screennames[$i]);
$xml[$i] = simplexml_load_string($xml[$i]);


It is not immediately a solution, but I cannot see any call to mysql_real_escape() in your code. This would make your code safer, for the case that somehow a ' gets in one of your strings.

For tracing down your bug, you should try to put some var_dump() or print_r() calls in order to know the values of the various arrays.

And, try to replace the constant values spread throughout your code by appropriate count() results.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号