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);
YvesLeBorg
  • 9,070
  • 8
  • 35
  • 48
VHK
  • 89
  • 5
  • 1
    try `console.log(JSON.stringify(data));` instead, the console will print objects as [Object, object] if you just print the object itself. – yvesmancera Aug 21 '15 at 16:46
  • Done. this is the log: {"readyState":4,"responseText":"\"67_18:52:54 21-8-2015\"","status":200,"statusText":"OK"} – VHK Aug 21 '15 at 16:53

3 Answers3

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)

Stryner
  • 7,288
  • 3
  • 18
  • 18
  • 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 Aug 21 '15 at 16:52
  • @VHK That's good, that means it's working, right? `66` is the id of the row? – Stryner Aug 21 '15 at 16:53
  • 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 Aug 21 '15 at 16:56
  • @VHK The issue is because [a string is not valid json](http://stackoverflow.com/questions/7487869/is-this-simple-string-considered-valid-json). See Martin's answer. – Stryner Aug 21 '15 at 17:02
  • That does make sense, but passing on a string would require the ajax datatype to be 'html' then, correct? – VHK Aug 21 '15 at 17:04
  • I would just omit `dataType` and echo the plain string. – Stryner Aug 21 '15 at 17:07
0

For the double double quotes problem use this:

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

And data.result in JS

Martin Joó
  • 325
  • 1
  • 11
  • What is undefined, data or data.result? Your php file is in UTF-8? – Martin Joó Aug 21 '15 at 17:12
  • data.result is undefined. and yes its in utf8. – VHK Aug 21 '15 at 17:14
  • 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. – Martin Joó Aug 21 '15 at 17:21
  • 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. – VHK Aug 21 '15 at 18:00
  • 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? – Martin Joó Aug 21 '15 at 18:04
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);
    }
});
VHK
  • 89
  • 5