开发者

Grabbing that all important data from mySQL from jQuery

开发者 https://www.devze.com 2023-02-08 12:25 出处:网络
I have the following jQuery statement: $.ajax({ type: \"POST\", url: \"db.php\", data: \"word=\"+ tmpWord,

I have the following jQuery statement:

$.ajax({
        type: "POST",
        url: "db.php",
        data: "word="+ tmpWord,
        success: function(){
            //somehow get 开发者_如何学Pythonthe word here ^_^
        }
    });

This is an excerpt from db.php:

$word = htmlspecialchars(trim($_POST['word']));
$addClient  = "SELECT * FROM dictionary WHERE word = " . $word;
mysql_query($addClient) or die(mysql_error());

My question to you good people is: How can I retrieve $word from db.php?

[ scratches head ]


[edit] Ok, so that was solved - Thanks sarfraz.
I'm now getting an error showing, here it is:

Unknown column 'apple' in 'where clause'

The query I am using looks like this:

$addClient = "SELECT * FROM dictionary WHERE word = 'apple'

And my sql is like this:

dictionary
|_ wID, word, definition, email, url, placeholder1, placeholder2

    0  apple  null        null   null     null        null

PHP MyAdmin states:

Showing rows 0 - 0 (1 total, Query took 0.0008 sec)
SQL query: SELECT *
FROM dictionary
WHERE word = 'apple'
LIMIT 0 , 30

When using: SELECT * FROM dictionary WHERE word = 'apple'


FINAL EDIT

THIS HAS BEEN SOLVED!

For some unknown reason I had to change trigger_error() to die().

Cheers.


Here is how your jQuery code should look like:

$.ajax({
        type: "POST",
        url: "db.php",
        data: "word="+ tmpWord,
        success: function(data){
            alert(data);
        }
});

And PHP Code:

$word = mysql_real_escape_string(trim($_POST['word']));
$query = "SELECT * FROM dictionary WHERE word = '$word'";
$result = mysql_query($query) or trigger_error(mysql_error()." ".$query);
$row = mysql_fetch_assoc($result);
// send back the word to ajax request
echo $row['word'];
0

精彩评论

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

关注公众号