开发者

PHP AJAX and mySQL not returning data?

开发者 https://www.devze.com 2023-02-08 16:43 出处:网络
I have the following block of PHP: $word = mysql_real_escape_string(trim($_GET[\'word\'])); $firstletter = substr(\'$word\', 0, 1);

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++;
   开发者_如何学Go }
    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

>:(


Try this

 $firstletter = substr($word, 0, 1);


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消