微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 特殊字符无法插入数据库

我是PHP新手.我试图读取文本文件并逐行插入数据库.我的问题是一些特殊的字符插入查询不起作用
例如Côte,d.ä.,d.y.,DAB-sändare这些都在起作用.但不能插入代办.如果我删除了d affaires然后查询将执行否则它将不会向数据库插入任何数据.我用来reaf并插入数据库PHP代码

MysqL_connect("localhost","root","");
    MysqL_select_db("testdb");
    $query="INSERT INTO keywords (id,keyword) VALUES ";
    $handle = fopen("Ordlista.txt","r");
    if ($handle) {
        $i=1;
        while (($line = fgets($handle)) !== false) {
            // process the line read.
           // echo $line.'MysqL_query($query);
    } else {
        echo 'error opening the file.';
        // error opening the file.
    } 
    fclose($handle);

更新
我在wordpress中创建插件时使用了此代码,然后特殊字符插入为“?”.在前面的代码中,我正在完成的代码更改是工作文件

MysqL_query("TruncATE TABLE $table");
//    $structure = "INSERT INTO $table (`id`,`keyword`) VALUES (NULL,'test1'),(NULL,'test2');"; // Keywords for Testing
//    $wpdb->query($structure);
   //read text file & insert to database start
   $query="INSERT INTO $table (id,keyword) VALUES ";
   $fllocation=PLG_URL.'/Ordlista.txt';
    $handle = fopen($fllocation,"r");
    if ($handle) {
        $i=1;
        while (($line = fgets($handle)) !== false) {
            // process the line read.
            if($i==1)
            {
            $query.=" ( NULL,'".MysqL_real_escape_string($line)."') ";
            $i++;
            }
            else {
                $query.=",'".MysqL_real_escape_string($line)."') ";
            }
        }
        $query.=";";
        $wpdb->query($query);
       // echo $query;
       // MysqL_query($query);
    } else {
        echo 'error opening the file.';
        // error opening the file.
    } 
    fclose($handle);
最佳答案
试试MysqL_real_escape_string();

MysqL_connect("localhost",'".MysqL_real_escape_string($line)."') ";
            }

        }
        $query.=";";
     //   $qr=htmlspecialchars($query,ENT_QUOTES);
        echo $query;
        MysqL_query($query);
    } else {
        echo 'error opening the file.';
        // error opening the file.
    } 
    fclose($handle);

原文地址:https://www.jb51.cc/mysql/433706.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐