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

时间:2014-04-13 11:57:03

标签: php mysql sql

我用这种方式用mysqli_real_escape将值插入表中:

    $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";
    }

我不会在这里得到错误。但是,当我从中选择数据进行处理时,会出现错误:

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

这是我收到错误的地方:

$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>";

}

我使用过mysql_real_escape,即使这显示出这种错误。我需要使用其他任何东西吗?

以下是完整代码:

    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 个答案:

答案 0 :(得分:1)

删除此

where word = '".$key."'

来自您对插入的查询。

在插入该键时,您不必在此处创建where子句,因此查询将自动查找插入的键。

  echo "$key : $value ";

应该是

 echo $key. " : ".$value ;