0

I'm attempting to access the properties of a JSON object. My ajax call is:

$.ajax({
  url: 'login.php',
  type: 'post',
  dataType: 'json',
  data: $('#frmLgn').serialize(),
  success: function(data) {
    console.log(data[0].message);
    console.log(data[1].message);
    console.log(data[2].message);
}

The PHP is:

for ($i = 0; $i < $queryMsgCntResults; $i++) {

  $queryGetNew = "SELECT message, msgID FROM $username WHERE isNew = 1;";
  try
  {
    $stmt = $db->prepare($queryGetNew);
    $stmt->execute();
    $message = $stmt->fetch(PDO::FETCH_ASSOC);
    $messageArray[] = $message;         
  } 
  catch(PDOException $ex)
  {
    die("Failed to run query: " . $ex->getMessage()); 
  }     
}
echo json_encode($messageArray);
}

I am expecting this to output:

    console.log(data[0].message); //contents of message1
    console.log(data[1].message); //contents of message2
    console.log(data[2].message); //contents of message3

But instead get:

    console.log(data[0].message); //contents of message1
    console.log(data[1].message); //contents of message1
    console.log(data[2].message); //contents of message1

What am I missing/messing up?

Please close this question. I'm going to re-organize, re-test, and quite likely re-post for clarity. I appreciate all of your assistance, and apologize for wasting anyone's time. Thank you.

6
  • Are you sure $messageArray is correct? Have you tried adding a print_r() to make sure? Commented Jun 26, 2013 at 16:49
  • 3
    In your for loop, you are running the same query over and over. Every time, you are getting the same results. Commented Jun 26, 2013 at 16:49
  • 1
    As your line "$message = $stmt->fetch(PDO::FETCH_ASSOC);" you are getting just the current row instead a rows array, first you must to iterate your result and store it on a array, and the sent this array to json Commented Jun 26, 2013 at 16:51
  • Where is $username set? Also, if you are using PDO, why are you concatenating the $username variable into the SQL string? Commented Jun 26, 2013 at 16:54
  • @RocketHazmat - I have additional PHP code within the same for loop that unsets the isNew flag, so once the first query is run, it won't return the same message. Sorry! Commented Jun 26, 2013 at 17:11

1 Answer 1

1
$message = $stmt->fetch(PDO::FETCH_ASSOC);

This line gets one row at a time. You need to keep calling it over and over to get every row.

while($message = $stmt->fetch(PDO::FETCH_ASSOC)){
    $messageArray[] = $message;
}
Sign up to request clarification or add additional context in comments.

3 Comments

@user2442072 this is the code block that I have commented before
I have additional code that that unsets the isNew flag, so the first iteration of the for loop retrieves the message and sets the isNew flag to 0, so on the second iteration, that message is not returned. I tried adding the additional code, but SO said I had too much code.
Then paste your code or something close for help you, cause as your post that your are requesting is a array results instead a simple row, and you need just a row, you just need "no iteration"

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.