1

The intention with the below code is to extract messages from a mysql table, and put each of them inside ONE array with {} around each output. Each output consists of various parameters as you can see, and is an array in itself. What the code does is that each time the loop is processed, in the JSON array that this later is converted into, it wraps the output in []´s, hence it´s now a new array which is created. What I get is:

[{"sender":"ll","message":"blah","timestamp":"2016-12-21 14:43:04","username":"","msgtype":"","threadid":"32629016712222016034323"},{"sender":"kk","message":"blahblah","timestamp":"2016-12-21 14:43:23","username":"","msgtype":"","threadid":"32629016712222016034323"},{"sender":"ll","message":"blahblahblah","timestamp":"2016-12-21 14:43:47","username":"","msgtype":"","threadid":"32629016712222016034323"}],[{"sender":"ll","message":"blahblahblahblah","timestamp":"2016-12-21 14:43:04","username":"","msgtype":"","threadid":"92337321312222016034304"},{"sender":"kk","message":"blahblahblahblahblah","timestamp":"2016-12-21 14:44:05","username":"","msgtype":"","threadid":"92337321312222016034304"}]]

And what I want is:

[{"sender":"ll","message":"blah","timestamp":"2016-12-21 14:43:04","username":"","msgtype":"","threadid":"32629016712222016034323"},{"sender":"kk","message":"blahblah","timestamp":"2016-12-21 14:43:23","username":"","msgtype":"","threadid":"32629016712222016034323"},{"sender":"ll","message":"blahblahblah","timestamp":"2016-12-21 14:43:47","username":"","msgtype":"","threadid":"32629016712222016034323"}],{"sender":"ll","message":"blahblahblahblah","timestamp":"2016-12-21 14:43:04","username":"","msgtype":"","threadid":"92337321312222016034304"},{"sender":"kk","message":"blahblahblahblahblah","timestamp":"2016-12-21 14:44:05","username":"","msgtype":"","threadid":"92337321312222016034304"}]

How do I proceed to get the right result here?

$data = array ();
foreach($threads as $threadid){
$sql = ("SELECT sender,message,timestamp,username,msgtype,threadid FROM    Messages WHERE  threadid = '$threadid' AND subject = '' AND timestamp >    '$newtimestamp' ORDER BY timestamp");
$arrayOfObjects = $conn->query($sql)->fetchAll(PDO::FETCH_OBJ);
$data[] = $$arrayOfObjects;
}

And FYI, $threadid is another array containing... threadids, and the loop correctly fetches these one by one, that´s not where the problem is.

Thanks in advance!!

4
  • ... and put each of them inside ONE array with {} around each output., are you trying to get json string? Commented Dec 25, 2016 at 20:21
  • can you specify what you mean with "inside ONE array with {} around each output"? Where/How do you want to see those brackets? As Json maybe? As {} indicates an object, not an array, it's a bit confusing what you want. Commented Dec 25, 2016 at 20:21
  • your need to define empty array var outside from foreach, and push array content inside of foreach. Commented Dec 25, 2016 at 20:22
  • I think you mean that: fetchAll(PDO::FETCH_OBJ) Commented Dec 25, 2016 at 20:23

3 Answers 3

1

You are doing O(N) database queries, consider doing just O(1) using an IN expression in your where clause. No need for a foreach loop and you'll get all your data in one array.

SELECT ... FROM Messages WHERE threadid IN (1, 2, 3, ...) AND ...

You might have to use a prepared statement for that.

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

6 Comments

So "WHERE threadid IN ('$threadid')"? Then I just get [] as output.
IN expects a comma separated list of ALL ids.
So then this method won´t work since the threadids need to be extracted from the mysql database. Or is there a way to generate such a comma separated list inside php instead of an array, and use it the way I´m doing it?
In your example the ids seem to be in $threads. Best use a prepared statement, read the documentation on who to do that.
Well, I decided to give your suggestion one more try, and managed to do it in a none prepared way. I´m aware that this is supposed to be risky, but so far this project just needs to work, then have the php codes updated to safer versions, and then go live:
|
0

I think you are searching for PDO::FETCH_OBJ. You had FETCH_ASSOC, which will return an array of associative arrays.
FETCH_OBJwill return an array ob stdObjects.

Also you reassigned $array to itself when doing $array[] = $array;..

$data = array();
foreach($threads as $threadid){
    $sql = ("SELECT sender,message,timestamp,username,msgtype,threadid FROM    Messages WHERE  threadid = '$threadid' AND subject = '' AND timestamp >    '$newtimestamp' ORDER BY timestamp");
    //                                             here it is:
    $arrayOfObjects = $conn->query($sql)->fetchAll(PDO::FETCH_OBJ);
    $data[] = $arrayOfObjects;
}

// now you can encode that as json and show it:
echo json_encode($data);

5 Comments

It´s indeed a JSON string I´m aiming for here, sorry to not be more clear about that.
I tried all the amendments but I get the same result. I´ve also updated the initial question so you can see what it is that I´m talking about.
the difference between what you get and what you want is that you still reassign $array to itself
You mean: $array[] = $array;? Well, I´ve changed the code in the script to exactly what you said but the output look the same.
Ok, I´ve updated the main message to reflect the changes made based upon your advise.
0

@akuhn

Well, I decided to give your suggestion one more try, and managed to do it in a none prepared way. I´m aware that this is supposed to be risky, but so far this project just needs to work, then have the php codes updated to safer versions, and then go live. It works, so thanks a bunch!

$sql = ("SELECT sender,message,timestamp,username,msgtype,threadid FROM Messages WHERE threadid IN ('" . implode("','",$threadid) . "') AND subject = '' AND timestamp > '$newtimestamp' ORDER BY timestamp");
$data = $conn->query($sql)->fetchAll(PDO::FETCH_OBJ);

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.