The function to see the output of an array, print_r($my_array); is a php and mysql database programmers best friend. Normally, examining the array will help in determining how an error was made in programming a query.
For example, duplicate or unsuspected values could result if one of the values in the array is not clearly defined. Imagine an instance for which you select an id, but not a specific id. Therefore, the array could output everything you wanted, but made 3 duplicate rows since the exact user_id was not identified in a WHERE condition or it was not necessary to have in the array.
Such array issues could occure in situations where you make a join by selecting data from 2 or more databse tables. If you select a column which is not used and could contain various auto_incremented IDs, your output could be 3 identical table rows with 3 indexed arrays. Since the one ID from was not clearly defined in one of the tables.
Here is an example:
“SELECT tb1.id, tb1.username, tb1.email, tb1.date, tb2.username, tb3.adminid from tb1 as tableone, tb2 as tabletwo, tb3 as table3 WHERE (tb1.username=tb2.username AND tb1.username=’username’) order by date DESC”;
With this query above, we would get the results we wanted which was the username and email for Mike. But, if tb3.adminid had 3 entries, there would be 3 indexed arrays with identical arrays.
Since there was no use for tb3.adminid, it would be best to leave it out of the query.
Alternatively, the query could have been written:
“SELECT tb1.id, tb1.username, tb1.email, tb1.date, tb2.username, tb3.adminid from tb1 as tableone, tb2 as tabletwo, tb3 as table3 WHERE (tb1.username=tb2.username AND tb1.username=’$username’ AND tb3.adminid=’$username’) order by date DESC”;
In the second query, the foreign key tb3.admin matched with other keys; thus only record was returned.