douba9425 2013-03-28 21:55 采纳率: 100%
浏览 67
已采纳

将数组插入数据库语法错误

I'm parsing article links from a friend's basketball website using DOM Parser. I want to store these values in my database but I'm getting a syntax error Here's the code:

<?php
include_once ('connect_to_mysql.php');
    include_once ('simple_html_dom.php');
    $html = file_get_html('http://basket-planet.com/ru/');
    $main = $html->find('div[class=mainBlock]', 0);
    $items = array();
        foreach ($main->find('a') as $m){
            $items[] = "$m->plaintext, $m->href";
        }
    //print_r($items);
    $reverse = array_reverse($items);
    print_r($reverse);

    $sql = mysql_query ("INSERT INTO basket_news (article, link) VALUES ".(implode(',', $reverse))."") or die (mysql_error());
?>

This is the output from the reverse array (sorry, it's in a nother language):

Array (
  [0] => 07:43 Видео. Дэвид Стерн и арбитры вручают "Лейкерс" победу над "Миннесотой" (1) , /ru/news/9234
  [1] => 07:51 "Чикаго" прервал победную серию "Майами" на отметке 27 (0) , /ru/news/9235
  [2] => 15:02 Кабмин выделил 200 млн грн на подготовку к Евробаскету (0) , /ru/news/9243
  [3] => 20:42 Евролига: ЦСКА ломает мадридский «Реал» (0) , /ru/news/9246
  [4] => 21:45 «Уникаха» побеждает в Стамбуле и молится на «Бамберг» (0) , /ru/news/9248 )

And here's the error:

You have an error in your SQL syntax; check the manual that corresponds to your 
MySQL server version for the right syntax to use near 
'07:43 Видео. Дэвид Стерн и ' at line 1

What am I doing wrong here? Please advise...

  • 写回答

2条回答 默认 最新

  • douping5226 2013-03-28 22:01
    关注

    The string is not delimited with quotes and not escaped in your code. The Values string should contained by ( and ) So the right way if you use mysql:

      $items[] = "('".mysql_real_escape_string($m->plaintext)."','".
                      mysql_real_escape_string($m->href)."')";
    

    And as the other commenters said, you should try out mysqli, or PDO in php. The query building is easier and safer than mysql_* functions. (And mysql_* functions will be deprecated in the next version of PHP)

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

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大