如何在PHP中将爆炸词与mysql varchar进行比较? 这段代码产生了我想要的东西,但它也给出了这个错误
veranderenwachtwoordjij
致命错误:在非对象上调用成员函数fetch_assoc()......
$word = "Also, to be safe, change your password regularly... you don't have to be obsessive about it: every three hours or so should be enough. And because erring on the side of caution is always a good idea, fake your own suicide and change your identity at least once a year.";
$pieces = explode(" ", $word);
$x = 0;
while($x < word_count($word)) { // word count function returns int (51)
$aPiece = $pieces[$x]; // change $pieces[$x] to 'you' and then it works
$result = $conn->query("SELECT * FROM dict WHERE english='$aPiece'");
$z = 0;
while($z < $num_result) // $num_result returns amount of rows in database
{
$row = $result->fetch_assoc(); //error line is here
echo stripslashes($row['dutch']);
$z++;
}
$x++;
}
答案 0 :(得分:1)
我猜这个问题来自你的测试句中的don't
:你忘了使用像mysql_real_escape_string
这样的函数来转义引号。
例如:
$aPiece = mysql_real_escape_string($pieces[$x]);
$result = $conn->query("SELECT * FROM dict WHERE english='$aPiece'");
答案 1 :(得分:0)
我认为
$row = $result2->fetch_assoc();
应该是
$row = $result->fetch_assoc();
因为你似乎没有任何$ result结果。
答案 2 :(得分:0)
$ result2中的$ result2-&gt; fetch_assoc();不是正确的变量,它应该是
$result->fetch_assoc();
顺便说一下,为了找到数据库中的单词,我必须删除标点符号(我认为)你的第一行应该(在爆炸之前)与此类似:
$words = strtr($words, ',.:!','');