dsm1998 2014-04-13 11:57
浏览 31
已采纳

插入值时,mysqli_real_escape看起来不正确

I am inserting value into table with mysqli_real_escape in this way:

    $noun = mysqli_real_escape_string($con,$noun);
    $adjective = mysqli_real_escape_string($con,$adjective );

    //$update1 = "UPDATE review_words SET adjective = CONCAT(adjective, ',', '$adjective'), noun = CONCAT(noun, ',', '$noun') ";
    $update1 = "UPDATE review_words SET adjective = CONCAT(IFNULL(adjective, ''), ',', '$adjective'), noun = CONCAT(IFNULL(noun, ''), ',', '$noun') ";          if (!mysqli_query($con,$update1))
    {
    //  die('Error: ' . mysqli_error($con));
    //  echo "error";
    }

I dont get error here. But when I select the data from that for processing it gives error :

restaurant : 16 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 'where word = 'restaurant'' at line 1

Here is where I get error:

$select1 = mysqli_query($con,"SELECT * from review_words");
while ($row = @mysqli_fetch_array($select1))
{
    $noun = $row['noun'];
    echo "Nons are  : $noun <br><br>";
    $adjective = $row['adjective'];     
    echo "Nons are  : $adjective <br>";

}

I have used mysql_real_escape, even this is showing this sort of error. Do i need to use anything else?

Here is full code:

    foreach($response->businesses as $business)
    {
        echo "<img border=0 src='".$business->image_url."'><br/>";
        echo "Local provider : ".$business->name."<br/>";
        $rtext = $business->snippet_text;
        echo "Review : ".$business->snippet_text."<br/>";
        if( $item = 'Italian_restaurants' or $item = 'Mexican_restaurants')
        {
            $keywords = MakeExternalReq($business->snippet_text);
            echo "<strong>Important keyword :  </strong>".$keywords."<br/>";
            $tagger = new PosTagger('lexicon.txt');
            $tags = $tagger->tag($rtext);
            $noun = printTagN($tags);
            $adjective = printTagA($tags);
            $noun = implode(", ",$noun);
            $adjective = implode(", ",$adjective);

            echo "Noun : $noun <br>";
            echo "Adjectives : $adjective <br>";

            //var_dump($var);

        }
        echo "Rate : ".$business->rating."<br/>";
        echo "Phone : ".$business->phone."<br/>";
        echo "Address : ".$business->location->display_address[0]."<br/>";
        echo "Category : ".$business->categories[0][0];     
        echo "<hr>";

        $brand = 'Yelp';
        $local_provider = $business->name;
        $review = $business->snippet_text;
        $id = md5($review);
        $rate = $business->rating;
        $image = $business->image_url;
        $phone = $business->phone;
        $address = $business->location->display_address[0];
        $category = $business->categories[0][0];

        $con = mysqli_connect('127.0.0.1', 'root', 'root', 'root');             
        if (mysqli_connect_errno())
        {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
            return;
        }

        $insertQuery1 = "INSERT INTO review_details(`id`,`brand`,`local_provider`,`review`,`rate`,`important_words`,`adjective`,`noun`,`image`,`phone`,`address`,`category`) VALUES ('".$id."','".$brand."','".$local_provider."','".$text."','".$rate."','".$keywords."','".$adjective."','".$noun."','".$image."','".$phone."','".$address."','".$category."')";

        if (!mysqli_query($con,$insertQuery1))
        {
        //  die('Error: ' . mysqli_error($con));
        //  echo "error";
        }

        $noun = mysqli_real_escape_string($con,$noun);
        $adjective = mysqli_real_escape_string($con,$adjective );

        //$update1 = "UPDATE review_words SET adjective = CONCAT(adjective, ',', '$adjective'), noun = CONCAT(noun, ',', '$noun') ";
        $update1 = "UPDATE review_words SET adjective = CONCAT(IFNULL(adjective, ''), ',', '$adjective'), noun = CONCAT(IFNULL(noun, ''), ',', '$noun') ";          if (!mysqli_query($con,$update1))
        {
        //  die('Error: ' . mysqli_error($con));
        //  echo "error";
        }
    }
}

function get_word_count()
{
    echo "<br> Entered into word count <br>";
    $con = mysqli_connect('127.0.0.1', 'root', 'root', 'root');             
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
        return;
    }
    $select1 = mysqli_query($con,"SELECT * from review_words");
    while ($row = @mysqli_fetch_array($select1))
    {
        $noun = $row['noun'];
        echo "Nons are  : $noun <br><br>";
        $adjective = $row['adjective'];     
        echo "Nons are  : $adjective <br>";

    }

    $noun_count = array_count_values(str_word_count($noun, 1));
    $adjective_count = array_count_values(str_word_count($adjective, 1));
    //echo $noun_count;

    arsort($noun_count);
    //print_r($noun_count);

    arsort($adjective_count);
    //print_r($adjective_count);


    //echo $noun_count;

    foreach($noun_count as $key=>$value)
    {
        echo "$key : $value ";
        $insertQuery2 = "INSERT INTO review_word_count (`word`,`count`,`type`) VALUES ('".$key."','".$value."','noun') ON DUPLICATE KEY UPDATE count = '".$value."' where word = '".$key."'";   
        if (!mysqli_query($con,$insertQuery2))
        {
            die('Error: ' . mysqli_error($con));
        //  echo "error";
        }
    }
    foreach($adjective_count as $key=>$value)
    {

        $insertQuery3 = "INSERT INTO review_word_count (`word`,`count`,`type`) VALUES ('".$key."','".$value."','adjective') ON DUPLICATE KEY UPDATE count = '".$value."' where word = '".$key."'";  
        if (!mysqli_query($con,$insertQuery3))
        {
            die('Error: ' . mysqli_error($con));
        //  echo "error";
        }
    }
    echo "<br> End  into word count <br>";


}
  • 写回答

1条回答 默认 最新

  • doumaojin4008 2014-04-13 12:09
    关注

    remove this

    where word = '".$key."'
    

    from your queries of insert.

    you dont have to make where clause here while you inserted that key, so the query will look for that inserted key automatically.

    also

      echo "$key : $value ";
    

    should be

     echo $key. " : ".$value ;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答