0

I am following a project for creating spell checker. However, rather than using regular mysql, i decided to go with PDO. So i converted code to PDO. I am stuck at one point and not sure why i can't call PDO inside any function even after declaring global variable. What i am doing wrong?

Purpose: I have loaded 100k+ words in a table and want to find similar words by searching one word.

    <?php

    include "db.inc.php";

    function spellcheck($word){
        global $db;
        $output = array();
        $word = $db->quote($word);

        $words = $db->prepare("SELECT words FROM english WHERE SUBSTRING(word, 0, 1) = '.substr ($word, 1, 2)'");
        $words->execute();

        while (($words_row = $words->fetch(PDO::FETCH_ASSOC)) !== false){
            echo $words_row['word'];
        }

        }


    if (isset($_GET["word"]) && trim($_GET["word"]) !== null){
        $word       = $_GET["word"];
        $spellcheck = spellcheck($word);
    }


    ?>

    <form action="" method="GET">
        Please type word to check: <input type="text" name="word">
        <input type="submit" value="Check">

    </form>
awebus
  • 19
  • 4
  • 1
    What's in db.inc.php? What error message do you receive? – EricP Jun 27 '14 at 20:21
  • 2
    Your quoting and concatenation is all wrong. `"SELECT words FROM english WHERE SUBSTRING(word, 0, 1) = '.substr ($word, 1, 2)'"` You are calling PHP `substr()` inside a double-quoted string. – Michael Berkowski Jun 27 '14 at 20:24
  • Please read over [How to squeeze an error message out of PDO](http://stackoverflow.com/questions/3726505/how-to-squeeze-error-message-out-of-pdo) and enable `PDO::ERRMODE_EXCEPTION` on your `$db` object so it always throws useful exceptions. By default, PDO errors silently. In this case, literally MySQL is comparing the `SUBSTRING()` output to the string `'.substr ($word, 1, 2)'`, not the output of a PHP function so the SQL is even valid (though it doesn't do at all what you intend) – Michael Berkowski Jun 27 '14 at 20:26
  • Can you describe exactly what you're trying to do with `SUBSTRING(word, 0, 1) = '.substr ($word, 1, 2)'` so we can point you in the right direction? – Michael Berkowski Jun 27 '14 at 20:28
  • @MichaelBerkowski, want to find similar words from database by searching a word. I have declared it as a variable separately now. I have enabled PDO::ERRMODE_EXCEPTION now. – awebus Jun 27 '14 at 20:38
  • @JoeCoder, Just database info. setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); ?> – awebus Jun 27 '14 at 20:42

1 Answers1

3

Try the following:

function spellcheck($word){

    $db = new PDO ("mysql:host=localhost;dbname=splcheck", "root", "");
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

    $query = "SELECT words FROM english WHERE SUBSTRING(word, 0, 1) = :word";
    $stmt = $db->prepare($query);
    $stmt->execute(array(':word'=> substr ($word, 1, 2)));

    $output = array();
    while ($words_row = $stmt->fetch(PDO::FETCH_ASSOC)){
        $output[] = $words_row['words'];
    }

    return $output;
}

  • Don't use global, pass the connection as argument instead
  • Make sure you prepare your query properly
  • Your function was not returning the output
  • Remove !== false from the while loop its redudant
  • Avoid typos you forgot s on $words_row['words'];

Using like statement:

$query = "SELECT `words` FROM english WHERE `word` = LIKE :word";
$stmt = $db->prepare($query);
$stmt->execute(array(':word'=>'%'.$word.'%'));
meda
  • 45,103
  • 14
  • 92
  • 122
  • i tried it that way earlier. Here is the error i got. (1) Fatal error: Call to a member function prepare() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/spell/index.php on line 7. – awebus Jun 27 '14 at 20:53
  • Ok just for the test of testing, create the db connection inside of your connection, I will edit now – meda Jun 27 '14 at 20:55
  • 1
    @user3768834 That means `$db` does not contain what you think it does in that scope. If it contained a valid connection before, be sure you have not closed it in the meantime (and don't forget to pass it to the function when you call it) – Michael Berkowski Jun 27 '14 at 20:56
  • @meda, thanks for followup, i tried it that way too by including "db.php" file before. It returns this error >> Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'o' in 'where clause'' in /Applications/XAMPP/xamppfiles/htdocs/spell/index.php – awebus Jun 27 '14 at 21:02
  • @user3768834 its complaining about the query , try `WHERE SUBSTRING(word, 1, 1)` – meda Jun 27 '14 at 21:05
  • @meda, i just noticed it sometime ago. It still doesn't solve the problem :( – awebus Jun 27 '14 at 21:08
  • @awebus check my update, you should run a query directly in the database to test – meda Jun 27 '14 at 21:27