Ok so I have this query, which works quite good, but it doesn't return what i want to.
SELECT acc.*, fin.*
FROM consumers acc
LEFT JOIN finances fin ON fin.uid = acc.uid
WHERE acc.id = '$userID';
This returns an array (in php) where the key is 0 and all the columns (from both tables) are under the same array.
What I want to achieve is get 2 arrays, 1 with columns from first table, and another 1 with columnds from the second table.
Basically i'm trying to get a multidimensional array out of 1 query.
Split it into two arrays after reading the row.
$row = mysqli_fetch_assoc($query);
$acc = array($row['col1'], $row['col2'], ...);
$fin = array($row['col6'], $row['col7'], ...);
where col1,
col2, ... are the columns from
consumer, and
col6,
col7, ... are the columns from
$finances`.
A way to do it without listing all the column names is with array_slice()
$row = mysqli_fetch_assoc($query);
$acc = array_slice($row, 0, $number_of_consumer_columns);
$fin = array_slice($row, $number_of_consumer_columns, $number_of_finances_columns);
$table3 = array_slice($row, $number_of_consumer_columns + $number_of_finances_columns, $number_of_table3_columns);
You can continue this for all the tables. It would be straightforward to put this into a function that takes an array of all the column counts, and returns a 2-dimensional array containing each table's data.
I've came up with a fast solution:
<?php
$tableData = array( 'account' => 'consumers', 'finance' => 'consumers_finance', 'household' => 'consumers_household', 'media' => 'consumers_media', 'medical' => 'consumers_medical', 'shopping' => 'consumers_shopping', 'social' => 'consumers_social', 'technology' => 'consumers_technology', 'transport' => 'consumer_transport', 'b2b' => 'consumers_b2b', 'employment' => 'consumer_employment_status');
$openAccount = array();
foreach($tableData as $key => $table)
{
$whichWhere = $db->doQuery("SHOW COLUMNS FROM `$table` LIKE 'uid'");
$whichWhere = (isset($whichWhere[0]['Field'])) ? 'uid' : 'id';
$getDetails = $db->getArray("SELECT * FROM $table WHERE $whichWhere = '$userID'");
if(isset($getDetails[0]))
{
$openAccount[$key] = $getDetails[0];
}
else
{
$getDetails = $db->doQuery("SHOW COLUMNS FROM `$table`");
foreach($getDetails as $column)
{
$openAccount[$key][$column[0]] = '';
}
}
}
?>
This will help me deal with tables that have no entry for uid so that i wont have to run double checks on array to avoid undefined indexes.