0

I'm trying to add an element to array, but I get a weird output. The code is the following:

$getalltokens = $db->query("SELECT * FROM Tables WHERE available = '$comp'");
    while ($row = $getalltokens->fetch(PDO::FETCH_ASSOC))
    {
        $fid = $row['FID'];
        $tok = $row['token'];
        $sql = $db->query("SELECT Firstname,Lastname FROM Users WHERE Token = '$tok'");
        $rez = $sql->fetch(PDO::FETCH_ASSOC);
        $names[] = $rez;
        $fidzy = array(
            'FID' => $fid
        );
        array_push($names, $fidzy);
    }
    $getalltokens = $db->query("SELECT FID FROM Tables WHERE available = '$comp'");
    $tokenz = $getalltokens->fetchAll(PDO::FETCH_ASSOC);
    echo json_encode($names);

And the output I get is:

[{"Firstname":"Test","Lastname":"Test"},{"FID":"5"},
{"Firstname":"Test2","Lastname":"Test2"},{"FID":"4"}]

While what I need is the FID to be inside the $names array, so it would be more like:

[{"Firstname":"Test","Lastname":"Test","FID":"5"}]
4
  • 1
    You don't need $fidzy there. Just do $names['FID'] = $fid; and get rid of array_push(). Commented Jun 16, 2015 at 2:49
  • @Ulver, you did same mistake like me. $names is array of arrays. Commented Jun 16, 2015 at 2:50
  • 1
    @ShamilYakupov Gotcha! Agreed. Commented Jun 16, 2015 at 2:52
  • This is an XY Problem. You shouldn't be performing iterated queries here. Use a JOIN clause in a properly constructed query to relate related data and avoid abusing system resources. Commented Feb 20 at 9:37

3 Answers 3

2
$rez['FID'] = $fid; /* Added */
$names[] = $rez;
/* $fidzy and array_push removed */
Sign up to request clarification or add additional context in comments.

4 Comments

The result of that is the following: {"0":{"Firstname":"Vukasin","Lastname":"Vulovic"},"FID":"9"} Do you think if I change the query to grab all the data at once, it would help? I'm gonna try that now
No worries, however, the new snippet results in the following: Notice: Undefined variable: names in /home/user/domain/api/getusers.php on line 46 null Line 46 is the echo json_encode($names); If I understood correctly, you're trying to add key and value to $rez which isn't an array? Since I'm doing the while for every token separately?
PDO::FETCH_ASSOC returning an array, so $rez is an array. Did you declare your $names before while? Also, I dont know what are you trying to do, but better to make it in one query like SELECT t.*, u.Firstname, u.Lastname FROM Tables as t, Users as u WHERE u.Token = t.token AND t.available = '$comp'.
I didn't delcare, that's what I'm looking to do but I'm pretty stuck here: $sql = $db->query("SELECT Users.Firstname,Users.Lastname FROM Users LEFT JOIN Tables.FID ON Users.Token = Tables.token WHERE Users.Token = '$tok'"); not the best left joiner I'll admit
1

You can use instead of array_push() like

$arrayname[indexname] = $value;

if you use array_push()

<?php
$array[] = $var;
?>
Note: If you use `array_push()` to add one element to the array it's

better to use$array[] = because in that way there is no overhead of calling a function.

Note: `array_push()` will raise a warning if the first argument is not an array. This differs from the `$var[]` behavior where a new array

is created.

Reference Array push

1 Comment

This is true, however, in this very specific case, a different solution is the best. Your suggestion does give the desired output, but changes formating or adds the FID without FID:1, just adds 1. I'll post the answer in a second
0

The solution to the specific problem at hand is selecting all the necessary data in a single query, removing the need to add elements to any array. This is done in the following fashion:

$sql = $db->query("SELECT
Users.Firstname,Users.Lastname,Tables.FID
FROM Users,Tables 
WHERE Users.Token = Tables.token");
$rez = $sql->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($rez);

1 Comment

I recommend not using old-skool comma joins. Modern programming leans toward JOIN with an ON or USING clause.

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.