My MySQL table "tracks" has these two fields:
Id (Int)
Name (Varchar 255)
The values are:
1 Day 1
2 Day 2
3 Day 3
I am trying to retrieve all the tracks from PHP using this functions:
function getTracks() {
$query = sprintf('select * from tracks');
return contentRetrieverDataBaseQuery($query)->fetchAssoc();
}
function contentRetrieverDataBaseQuery($query) {
$settings = contentRetrieverGetSettings();
Database::addConnectionInfo('contentRetriever', 'default', $settings['database_connection']);
db_set_active('contentRetriever');
$result = db_query($query);
db_set_active();
return $result;
}
Running select * from event_tracks on phpmyadmin I get the results correctly.
When I do this:
$tracks = getTracks();
foreach ($tracks as $track) {
echo $track['name'];
}
I get as an output "1D", which is the Id and the first letter of the Name.
What am I doing wrong?
EDIT: Corrected the name of the database
contentRetrieveDataBaseQuerydo you define$result...$result = db_query($query);->fetchAssoc()is returning a SINGLE row of data, which means that$tracksis a single level array, and$track['name']is taking the string value in $track and trying to treat it as a string, meaning$foo = 'bar'; echo $foo[1]outputsa. -['name']will be parsed down to[0].fetchAllinstead offetchAssocto get all the results as a multi-dimensional array.