PHP字符串不更新mySQL表

时间:2016-07-26 05:50:18

标签: php mysql string

环境:
PHP 7,mySQL,phpMyAdmin,localhost,MAMP,Mac

问题:

我正在解析一些html文本到我的localhost上的mySQL数据库,字符串似乎没有按计划更新表。

我测试了代码,并且能够将问题root缩小为我想要在mySQL表中更新的变量中包含的字符串。

我通过将变量中的字符串手动替换为“测试测试”来测试变量和查询语法,并在运行代码时成功加载。

我也尝试将mySQL表结构从'text'更改为'long text',即使字符串显然足够短以适合文本字段但是这没有帮助。

我相信问题在于字符串本身以及可能的表/字段结构。

值得一提的是,查询更新通过此代码与其他字符串一起使用,并且只在某些情况下失败(尽管经常发生)

我在问什么

此时,在尝试了一些事情并研究信息之后,我对这个问题可能会有点困惑,所以如果你以前遇到过这个问题,任何建议都会有用。 下面是关于代码和字符串的更多信息。

信息

这是字符串:

$Definition = verb (used without object) , abode or abided, abiding. 1. to remain; continue; stay: abide with me. 2. to have one's abode; dwell; reside: to abide in a small scottish village. 3. to continue in a particular condition, attitude, relationship, etc.; last. verb (used with object) , abode or abided, abiding. 4. to put up with; tolerate; stand: i can't abide dishonesty! 5. to endure, sustain, or withstand without yielding or submitting: to abide a vigorous onslaught. 6. to wait for; await: to abide the coming of the lord. 7. to accept without opposition or question: to abide the verdict of the judges. 8. to pay the price or penalty of; suffer for. verb phrases 9. abide by, to act in accord with. to submit to; agree to: to abide by the court's decision. to remain steadfast or faithful to; keep: if you make a promise, abide by it.;

代码:

        $Definition = implode('', $InterArray); //I also tested the array to string conversion works fine
        //$Definition = 'Test test test'; I tested this and it worked
        $Definition = (string) $Definition;

        $conn = mysqli_connect("localhost:8889","root","root","myDB");              
        $sql="INSERT INTO myTable (DEFINITION) VALUES('".$Definition."');";             
        $conn->query($sql);

SQL表: 定义,类型:长文本,排序规则:latin_swedish_ci,默认值:无

1 个答案:

答案 0 :(得分:-2)

为避免混淆,最好使用mysqli_real_escape_string()而不是手动转义字符串。 mysqli_real_escape_string()函数负责转义本身。

根据php.net

“此函数用于创建可在SQL语句中使用的合法SQL字符串。给定的字符串将编码为转义的SQL字符串,并考虑连接的当前字符集。”

马格努斯说,准备好的声明永远是最好的选择。你永远不能确定你的应用程序的安全性。