0

I have the following block of PHP:

$word = mysql_real_escape_string(trim($_GET['word']));
    $firstletter = substr('$word', 0, 1);

    $query = "SELECT * FROM `dictionary` WHERE word LIKE '%$firstletter'";
    $result = mysql_query($query) or die(mysql_error().": ".$query);
    $row = mysql_fetch_assoc($result);
    // send back the word to ajax request
    $i = 0;
    $fullLoad = '';
    while ($i < mysql_numrows($result)) {
        $fullLoad = $fullload . '|' . $row['word'];
        $i++;
    }
    echo $fullLoad;

Now, my AJAX call:

$.ajax({
                type: "GET",
                url: "word-list.php",
                data: "word="+ theword,
                success: function(data){ //data retrieved
                    console.log(data);
                            }
    });

Now, lets assume that the missing word variable is apple - so $word = 'apple';

But when the console.log() outputs - all I get is zero, nothing, nada, zip, blahblah

>:(

4
  • Does it work if you browse to word-list.php?word=apple? Commented Feb 6, 2011 at 14:50
  • no, I just get a blank window - no source :( Commented Feb 6, 2011 at 14:53
  • 1
    substr('$word', 0, 1); is not the same as substr("$word", 0, 1);, if you substr '$word' between 0 and 1, the firstletter is $, if you substr "$word" or $word between 0 and 1, this can be different (a of apple or sth. like that), and if your DB returns null (because there is nothing with $) this is normal. Change '$word' to $word or "$word" ;) Commented Feb 6, 2011 at 14:58
  • Thank you for the further explanation (+1) Commented Feb 6, 2011 at 15:07

2 Answers 2

6

Try this

 $firstletter = substr($word, 0, 1);
Sign up to request clarification or add additional context in comments.

1 Comment

yeah. that it ;) '$word' != "$word" and $word ;)
2

I'm a bit confused by this logic here:

$row = mysql_fetch_assoc($result);
$i = 0;
$fullLoad = '';
while ($i < mysql_numrows($result)) {
    $fullLoad = $fullload . '|' . $row['word'];
    $i++;
}
echo $fullLoad;

mysql_fetch_assoc will only ever return one row. Don't you mean:

$fullLoad = '';
while ($row = mysql_fetch_assoc($result)) {
    if( !is_null( $row['word'] ) ) $fullLoad .= . '|' . $row['word'];
}
echo $fullLoad;

The first one will only ever output one result a number of different times. The second example will output all of the values so long as a row's word value is not null.

1 Comment

oh man, I was JUST wondering why there was only one row! Thank you! Shame I can't accept two answers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.