tags:

views:

228

answers:

3

How can I compare exploded word with mysql varchar in PHP?
This code produce what i want but it also give this error

veranderenwachtwoordjij
Fatal error: Call to a member function fetch_assoc() on a non-object......

$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++;
}
A: 

I think

$row = $result2->fetch_assoc();

should be

$row = $result->fetch_assoc();

since you don't seem to have a $result anywhere.

pixel
oops..it's a typoshould be $row = $result->fetch_assoc();
azam
+1  A: 

I guess the problem comes from the don't in your test sentence : you forgot to escape quotes with a function like mysql_real_escape_string.

For example :

$aPiece = mysql_real_escape_string($pieces[$x]);
$result = $conn->query("SELECT * FROM dict WHERE english='$aPiece'");
Pierre Bourdon
A: 

$result2 in $result2->fetch_assoc(); is not the correct variable, it should be

$result->fetch_assoc();

by the way you have to delete the punctuation marks in order to find the words in your database (I think) one of your first lines should (before the explode) be similar to this:

$words = strtr($words, ',.:!','');
Eineki
it is already in place in word_count function :)
azam