MySQL 语法错误是由奇怪的数组值引起的,该数组值不应该存在?
这真的非常非常奇怪。
基本上,由于该字符串中的引号,我发生了 MySQL 语法错误:
('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')
更具体地说,这部分,其中与 Justin Bieber 相关的部分不应该在那里:
('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')
这是我的实际代码:
$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];
作为参考,它查询 Twitter API,检索特定的有关每个 $screenname
值的数据,然后将其连接成 (...) VALUES $value_string
的一个大字符串,但是在 $value[13] 似乎只是将
$value[1]
放在它的中间。
到目前为止我已经尝试过;
使用 implode (总是返回函数错误)。
只需在第一个 for 循环中使用 $value_string .=$value[$i].","
即可留下尾随 ,
。
更改 $screennames
值。
我只是不明白是什么导致了这个问题。
任何帮助/答案/评论将非常非常非常感谢:)!!
更新 1
这是我的代码的最后部分,为了更好地衡量:
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);
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
固定:
从第一个 for 循环向下替换
也会删除第二个 for 循环,这是毫无意义的。
fixed:
replace from first for loop down
also removes the 2nd for loop which is rather pointless.
如果我在使用 $value 之前将其声明为数组,那么问题就消失了:
解释:我认为问题是 $value 首先在 foreach 中用作标量变量($followers 段,然后用作数组之后,奇怪的组合
@Pixelatron。如果你想绕过速率限制,只需将结果缓存在临时文件中进行测试..
然后将其注释掉,然后加载缓存而不是加载远程xml。
The problem disappear for me if I declare $value as an array before using it:
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..
and then comment that off, and load the cache instead of the loading the remote xml
这不是立即的解决方案,但我在您的代码中看不到任何对 mysql_real_escape() 的调用。这将使您的代码更安全,以防
'
以某种方式进入您的字符串之一。为了追踪错误,您应该尝试进行一些
var_dump()
或print_r()
调用,以便了解各个数组的值。并且,尝试用适当的
count()
结果替换整个代码中分布的常量值。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()
orprint_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.