2011-02-06 64 views
0

我有PHP的以下塊:PHP AJAX和mySQL不返回數據?

$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; 

現在,我的AJAX調用:

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

現在,讓我們假設所缺少的單詞變量是apple - 所以$word = 'apple';

但是,當console.log()輸出 - 所有我得到的是零,沒有,虛無,拉鍊,blahblah

> :(

+0

如果你瀏覽到word-list.php?word = apple? – 2011-02-06 14:50:04

+0

不,我只是得到一個空白的窗口 - 沒有來源:( – 2011-02-06 14:53:12

+1

是錯誤報告? – 2011-02-06 14:54:41

回答

6

試試這個

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

我有點此地此邏輯困惑:

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

mysql_fetch_assoc僅會返回一行。你是不是這個意思:

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

第一個只會輸出一個結果許多不同的時間。第二個例子將輸出所有的值,只要一行的字值不爲空即可。