0

I try to put a string to $qDateTime and print it out. I expect the result is Array ( [0] => 2013-12-16 08:38:33 [1] => 2013-12-16 08:44:58 ). But it print Array ( [0] => 2013 [1] => 2013 ). And the $temp can print out correctly like 2013-12-16 08:38:332013-12-16 08:44:58.

    $qDateTime = array();

    $query = "SELECT joinDateTime FROM queueItem WHERE uid = '".$uid."' AND isValid = true;";
    $result = mysql_query($query) or die("Fail");
    while($array = mysql_fetch_array($result)){
        $temp = $array['joinDateTime'];
        $qDateTime[] += $temp;
        echo $temp;

    }
    print_r($qDateTime);
2
  • 2
    Correct syntax for pushing into array is $qDateTime[] = $temp; Commented Dec 16, 2013 at 16:12
  • $qDateTime[] = $temp, not +=. Commented Dec 16, 2013 at 16:12

3 Answers 3

3
    $temp = $array['joinDateTime'];
    $qDateTime[] += $temp;
    echo $temp;

You're assigning data to the array wrongly:

    $qDateTime[] += $temp;

Should be:

    $qDateTime[] = $temp;

Because you're using += in your code, PHP is converting the date string to an integer, and using that for the result; so your 2013-12-16 08:38:332013-12-16 is becoming just 2013.

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

Comments

0

You should use $qDateTime[] = $temp; instead of $qDateTime[] += $temp;

    while($array = mysql_fetch_array($result)){
        $temp = $array['joinDateTime'];
        $qDateTime[] = $temp;
        echo $temp;

    }

    var_dump($qDateTime);

Comments

0

The way of data assignment to array is incorrect. it should be,

$qDateTime[] = $temp; instead of $qDateTime[] += $temp;

print_r($qDateTime);

So the correct code must be,

  $qDateTime = array();

    $query = "SELECT joinDateTime FROM queueItem WHERE uid = '".$uid."' AND isValid = true;";
    $result = mysql_query($query) or die("Fail");
    while($array = mysql_fetch_array($result)){
        $temp = $array['joinDateTime'];
        $qDateTime[] = $temp;
        echo $temp;

    }
    print_r($qDateTime);

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.