0

I want the success function to pick up the string it should receive from the PHP file.

The PHP complains "mysql_fetch_array() expects parameter 1 to be a resource, boolean given instead". I assume this is why the success function does not fire.

What am I doing wrong?

The jQuery:

var string = "something to be inserted";
$.ajax({
    url: '...',
    type: 'post',
    dataType: 'json',
    data: {toBeInserted: string.toLowerCase()},
    success: function(data) {
        console.log(data);
        // some code that is to work with data
    }
});

The PHP:

include 'serverStuff.php';
// A separate file with $con defined in it. Assume this works.

mysql_query("SET NAMES 'utf8'", $con);

// inserts the $_POST['toBeInserted'] into the database just fine

// assume the following are defined: 
// (string) $user_name, (string) $now, (string) $statement

$sql=("SELECT * FROM table WHERE user_name=$user_name AND date=$now AND statement = $statement");
$result=mysql_query($sql, $con);

if ($row = mysql_fetch_array($result)) {
    $new_id = (int) $row['id'];
}

mysql_close($con);

echo json_encode($new_id.'_'.$now);
2
  • 1
    try console.log(JSON.stringify(data)); instead, the console will print objects as [Object, object] if you just print the object itself. Commented Aug 21, 2015 at 16:46
  • Done. this is the log: {"readyState":4,"responseText":"\"67_18:52:54 21-8-2015\"","status":200,"statusText":"OK"} Commented Aug 21, 2015 at 16:53

3 Answers 3

2

Strings need to be escaped in SQL:

"SELECT * FROM table WHERE user_name='$user_name' AND date='$now' AND statement = '$statement'"

It says "boolean given" because by the specification false is returned if there is an error. (Also consider heeding the deprecation note in the documentation)

Sign up to request clarification or add additional context in comments.

6 Comments

Escaped them, and got a different response text this time: ""66_18:49:55 21-8-2015"". Yes, double double quotes. These come from the DB though, so that much is working now.
@VHK That's good, that means it's working, right? 66 is the id of the row?
It is reaching the database, yes, but the jQuery does not pick up the returned object as a string, so the follow up code is not able to work. Yvesmancera asked me to log the object stringified: {"readyState":4,"responseText":"\"67_18:52:54 21-8-2015\"","status":200,"statusText":"OK"}. Does this tell you anything I'm missing? And yes: the 66/67/and so on are the concatenated ID's.
@VHK The issue is because a string is not valid json. See Martin's answer.
That does make sense, but passing on a string would require the ajax datatype to be 'html' then, correct?
|
0

For the double double quotes problem use this:

echo json_encode(array('result' => $new_id.'_'.$now));

And data.result in JS

5 Comments

What is undefined, data or data.result? Your php file is in UTF-8?
data.result is undefined. and yes its in utf8.
Hmm. And how data looks like? Try with some static data. array('result' => "some text") if it works, then something wrong with the concatenated data.
Sorry for my absence, Martin. I had to eat. I did what you asked and got this in the console.log: Object {readyState: 4, responseText: "test", status: 200, statusText: "OK"}. So it clearly does get passed, but not in a way the success function can pick up the string and work with it.
responseText: "test". in my opinion you'd like to use arrays. What happens when you try with echo json_encode(array('result' => "test")) ? And what is typeof data in this case in success?
0

Alright, I'm still not sure what causes the problem, but the workaround is the following:

  • switched the ajax function's datatype from 'json' to 'html';
  • changed the echo(json_encode("string")) to just echo("string");
  • picked up the string in the success function as follows:

The code:

$.ajax({
    // ...
    success: function(data) {
        console.log(data.responseText);
    }
});

Comments

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.