I'm trying to json_encode my array I'm getting back so I can place it inside of a jQuery plugin for the data source. My issue is that I am using PDO to query my database to get back the array, but upon doing a print_r, I see that every name I'm getting back from my database is in its own array. How would I place all these results into a single array so that when I do my json_encode it is all in one readable string for the jQuery plugin?
Database Query (PDO) -
$query = "
SELECT name
FROM `clients`
";
try
{
$stmt = $b3->prepare($query);
$stmt->execute();
}
catch(PDOException $ex)
{
die("Failed to run query: " . $ex->getMessage());
}
$players = $stmt->fetchAll();
Example of the returned array upon doing (Note I have 8000 getting returned so I will only post the first 5 or so)
print_r($players);
Array
(
[0] => Array
(
[name] => ! CBC.ZXR
)
[1] => Array
(
[name] => ! marioxz
)
[2] => Array
(
[name] => ! V v :]
)
[3] => Array
(
[name] => !?!
)
[4] => Array
(
[name] => !CU @ 1337
)
So more or less, how would I unify my array, so that when I do
json_encode($players["name"]);
It will return back a single JSON string of the names mentioned above.
Edit
Current Code,
$query = "
SELECT name
FROM `clients`
";
try
{
$stmt = $b3->prepare($query);
$stmt->execute();
}
catch(PDOException $ex)
{
die("Failed to run query: " . $ex->getMessage());
}
$playerNames = $stmt->fetchAll(PDO::FETCH_COLUMN, 0);
json_encode($playerNames);
var_dump(json_last_error());
json_encodereturns a value, it doesn't transform its argument. Store this value ($playerNamesJson = json_encode($playerNames);) and/or echo it. Right now you're just throwing the result away.autocompleteneeds, usually only a small subset (which matches the pattern) is returned.